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
51 private final int UPPER_LIMIT = 2048;
52
53 private StringBuffer buf = new StringBuffer(DEFAULT_SIZE);
54
55 private boolean locationInfo = false;
56
57 private boolean properties = false;
58
59
60
61
62
63
64
65
66
67
68 public void setLocationInfo(boolean flag) {
69 locationInfo = flag;
70 }
71
72
73
74
75
76
77 public boolean getLocationInfo() {
78 return locationInfo;
79 }
80
81
82
83
84
85
86 public void setProperties(final boolean flag) {
87 properties = flag;
88 }
89
90
91
92
93
94
95 public boolean getProperties() {
96 return properties;
97 }
98
99 @Override
100 @SuppressWarnings("unchecked")
101 public String format(final LogRecord record) {
102
103
104 if (buf.capacity() > UPPER_LIMIT) {
105 buf = new StringBuffer(DEFAULT_SIZE);
106 } else {
107 buf.setLength(0);
108 }
109 buf.append("<log4j:event logger=\"");
110 buf.append(Transform.escapeTags(record.getLoggerName()));
111 buf.append("\" timestamp=\"");
112 buf.append(record.getMillis());
113 buf.append("\" level=\"");
114
115 buf.append(Transform.escapeTags(record.getLevel().getName()));
116 buf.append("\" thread=\"");
117 buf.append(String.valueOf(record.getThreadID()));
118 buf.append("\">\r\n");
119
120 buf.append("<log4j:message><![CDATA[");
121
122
123 Transform.appendEscapingCDATA(buf, record.getMessage());
124 buf.append("]]></log4j:message>\r\n");
125
126 if (record.getThrown() != null) {
127 String[] s = Transform.getThrowableStrRep(record.getThrown());
128 if (s != null) {
129 buf.append("<log4j:throwable><![CDATA[");
130 for (String value : s) {
131 Transform.appendEscapingCDATA(buf, value);
132 buf.append("\r\n");
133 }
134 buf.append("]]></log4j:throwable>\r\n");
135 }
136 }
137
138 if (locationInfo) {
139 buf.append("<log4j:locationInfo class=\"");
140 buf.append(Transform.escapeTags(record.getSourceClassName()));
141 buf.append("\" method=\"");
142 buf.append(Transform.escapeTags(record.getSourceMethodName()));
143 buf.append("\" file=\"?\" line=\"?\"/>\r\n");
144 }
145
146 if (properties) {
147 Map contextMap = MDC.getCopyOfContextMap();
148 if (contextMap != null) {
149 Set keySet = contextMap.keySet();
150 if ((keySet != null) && (keySet.size() > 0)) {
151 buf.append("<log4j:properties>\r\n");
152 Object[] keys = keySet.toArray();
153 Arrays.sort(keys);
154 for (Object key1 : keys) {
155 String key = (key1 == null ? "" : key1.toString());
156 Object val = contextMap.get(key);
157 if (val != null) {
158 buf.append("<log4j:data name=\"");
159 buf.append(Transform.escapeTags(key));
160 buf.append("\" value=\"");
161 buf.append(Transform.escapeTags(String.valueOf(val)));
162 buf.append("\"/>\r\n");
163 }
164 }
165 buf.append("</log4j:properties>\r\n");
166 }
167 }
168
169 }
170 buf.append("</log4j:event>\r\n\r\n");
171
172 return buf.toString();
173 }
174
175 }