1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.log4j.xml;
21
22 import org.apache.log4j.Layout;
23 import org.apache.log4j.helpers.Transform;
24 import org.apache.log4j.spi.LocationInfo;
25 import org.apache.log4j.spi.LoggingEvent;
26
27 import java.util.Set;
28 import java.util.Arrays;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class XMLLayout extends Layout {
69
70 private final int DEFAULT_SIZE = 256;
71 private final int UPPER_LIMIT = 2048;
72
73 private StringBuffer buf = new StringBuffer(DEFAULT_SIZE);
74 private boolean locationInfo = false;
75 private boolean properties = false;
76
77
78
79
80
81
82
83
84
85
86
87
88 public void setLocationInfo(boolean flag) {
89 locationInfo = flag;
90 }
91
92
93
94
95 public boolean getLocationInfo() {
96 return locationInfo;
97 }
98
99
100
101
102
103
104 public void setProperties(final boolean flag) {
105 properties = flag;
106 }
107
108
109
110
111
112
113 public boolean getProperties() {
114 return properties;
115 }
116
117
118 public void activateOptions() {
119 }
120
121
122
123
124
125 public String format(final LoggingEvent event) {
126
127
128
129 if(buf.capacity() > UPPER_LIMIT) {
130 buf = new StringBuffer(DEFAULT_SIZE);
131 } else {
132 buf.setLength(0);
133 }
134
135
136
137 buf.append("<log4j:event logger=\"");
138 buf.append(Transform.escapeTags(event.getLoggerName()));
139 buf.append("\" timestamp=\"");
140 buf.append(event.timeStamp);
141 buf.append("\" level=\"");
142 buf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
143 buf.append("\" thread=\"");
144 buf.append(Transform.escapeTags(event.getThreadName()));
145 buf.append("\">\r\n");
146
147 buf.append("<log4j:message><![CDATA[");
148
149
150 Transform.appendEscapingCDATA(buf, event.getRenderedMessage());
151 buf.append("]]></log4j:message>\r\n");
152
153 String ndc = event.getNDC();
154 if(ndc != null) {
155 buf.append("<log4j:NDC><![CDATA[");
156 Transform.appendEscapingCDATA(buf, ndc);
157 buf.append("]]></log4j:NDC>\r\n");
158 }
159
160 String[] s = event.getThrowableStrRep();
161 if(s != null) {
162 buf.append("<log4j:throwable><![CDATA[");
163 for(int i = 0; i < s.length; i++) {
164 Transform.appendEscapingCDATA(buf, s[i]);
165 buf.append("\r\n");
166 }
167 buf.append("]]></log4j:throwable>\r\n");
168 }
169
170 if(locationInfo) {
171 LocationInfo locationInfo = event.getLocationInformation();
172 buf.append("<log4j:locationInfo class=\"");
173 buf.append(Transform.escapeTags(locationInfo.getClassName()));
174 buf.append("\" method=\"");
175 buf.append(Transform.escapeTags(locationInfo.getMethodName()));
176 buf.append("\" file=\"");
177 buf.append(Transform.escapeTags(locationInfo.getFileName()));
178 buf.append("\" line=\"");
179 buf.append(locationInfo.getLineNumber());
180 buf.append("\"/>\r\n");
181 }
182
183 if (properties) {
184 Set keySet = event.getPropertyKeySet();
185 if (keySet.size() > 0) {
186 buf.append("<log4j:properties>\r\n");
187 Object[] keys = keySet.toArray();
188 Arrays.sort(keys);
189 for (int i = 0; i < keys.length; i++) {
190 String key = keys[i].toString();
191 Object val = event.getMDC(key);
192 if (val != null) {
193 buf.append("<log4j:data name=\"");
194 buf.append(Transform.escapeTags(key));
195 buf.append("\" value=\"");
196 buf.append(Transform.escapeTags(String.valueOf(val)));
197 buf.append("\"/>\r\n");
198 }
199 }
200 buf.append("</log4j:properties>\r\n");
201 }
202 }
203
204 buf.append("</log4j:event>\r\n\r\n");
205
206 return buf.toString();
207 }
208
209
210
211
212
213 public boolean ignoresThrowable() {
214 return false;
215 }
216 }