1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.util;
21
22 import java.util.Arrays;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.logging.Formatter;
26 import java.util.logging.LogRecord;
27
28 import org.slf4j.MDC;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class Log4jXmlFormatter extends Formatter {
48
49 private final int DEFAULT_SIZE = 256;
50 private final int UPPER_LIMIT = 2048;
51
52 private StringBuffer buf = new StringBuffer(DEFAULT_SIZE);
53 private boolean locationInfo = false;
54 private boolean properties = false;
55
56
57
58
59
60
61
62
63
64
65 public void setLocationInfo(boolean flag) {
66 locationInfo = flag;
67 }
68
69
70
71
72
73
74 public boolean getLocationInfo() {
75 return locationInfo;
76 }
77
78
79
80
81
82
83 public void setProperties(final boolean flag) {
84 properties = flag;
85 }
86
87
88
89
90
91
92 public boolean getProperties() {
93 return properties;
94 }
95
96 @Override
97 @SuppressWarnings("unchecked")
98 public String format(final LogRecord record) {
99
100
101 if (buf.capacity() > UPPER_LIMIT) {
102 buf = new StringBuffer(DEFAULT_SIZE);
103 } else {
104 buf.setLength(0);
105 }
106 buf.append("<log4j:event logger=\"");
107 buf.append(Transform.escapeTags(record.getLoggerName()));
108 buf.append("\" timestamp=\"");
109 buf.append(record.getMillis());
110 buf.append("\" level=\"");
111
112 buf.append(Transform.escapeTags(record.getLevel().getName()));
113 buf.append("\" thread=\"");
114 buf.append(String.valueOf(record.getThreadID()));
115 buf.append("\">\r\n");
116
117 buf.append("<log4j:message><![CDATA[");
118
119
120 Transform.appendEscapingCDATA(buf, record.getMessage());
121 buf.append("]]></log4j:message>\r\n");
122
123 if (record.getThrown() != null) {
124 String[] s = Transform.getThrowableStrRep(record.getThrown());
125 if (s != null) {
126 buf.append("<log4j:throwable><![CDATA[");
127 for (String value : s) {
128 Transform.appendEscapingCDATA(buf, value);
129 buf.append("\r\n");
130 }
131 buf.append("]]></log4j:throwable>\r\n");
132 }
133 }
134
135 if (locationInfo) {
136 buf.append("<log4j:locationInfo class=\"");
137 buf.append(Transform.escapeTags(record.getSourceClassName()));
138 buf.append("\" method=\"");
139 buf.append(Transform.escapeTags(record.getSourceMethodName()));
140 buf.append("\" file=\"?\" line=\"?\"/>\r\n");
141 }
142
143 if (properties) {
144 Map contextMap = MDC.getCopyOfContextMap();
145 if (contextMap != null) {
146 Set keySet = contextMap.keySet();
147 if (( keySet != null ) && ( keySet.size() > 0 )) {
148 buf.append("<log4j:properties>\r\n");
149 Object[] keys = keySet.toArray();
150 Arrays.sort(keys);
151 for (Object key1 : keys) {
152 String key = (key1 == null?"":key1.toString());
153 Object val = contextMap.get(key);
154 if (val != null) {
155 buf.append("<log4j:data name=\"");
156 buf.append(Transform.escapeTags(key));
157 buf.append("\" value=\"");
158 buf.append(Transform.escapeTags(String.valueOf(val)));
159 buf.append("\"/>\r\n");
160 }
161 }
162 buf.append("</log4j:properties>\r\n");
163 }
164 }
165
166 }
167 buf.append("</log4j:event>\r\n\r\n");
168
169 return buf.toString();
170 }
171
172 }