1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.layout;
18
19 import java.io.IOException;
20 import java.io.InterruptedIOException;
21 import java.io.LineNumberReader;
22 import java.io.PrintWriter;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.lang.management.ManagementFactory;
26 import java.nio.charset.Charset;
27 import java.util.ArrayList;
28 import org.apache.logging.log4j.Level;
29 import org.apache.logging.log4j.core.Layout;
30 import org.apache.logging.log4j.core.LogEvent;
31 import org.apache.logging.log4j.core.config.Node;
32 import org.apache.logging.log4j.core.config.plugins.Plugin;
33 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
34 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
35 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
36 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
37 import org.apache.logging.log4j.core.util.Charsets;
38 import org.apache.logging.log4j.core.util.Constants;
39 import org.apache.logging.log4j.core.util.Transform;
40
41
42
43
44
45
46
47
48 @Plugin(name = "HtmlLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
49 public final class HtmlLayout extends AbstractStringLayout {
50
51 private static final long serialVersionUID = 1L;
52
53 private static final int BUF_SIZE = 256;
54
55 private static final String TRACE_PREFIX = "<br /> ";
56
57 private static final String REGEXP = Constants.LINE_SEPARATOR.equals("\n") ? "\n" : Constants.LINE_SEPARATOR + "|\n";
58
59 private static final String DEFAULT_TITLE = "Log4j Log Messages";
60
61 private static final String DEFAULT_CONTENT_TYPE = "text/html";
62
63 public static final String DEFAULT_FONT_FAMILY = "arial,sans-serif";
64
65 private final long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
66
67
68 private final boolean locationInfo;
69
70 private final String title;
71
72 private final String contentType;
73
74
75 public static enum FontSize {
76 SMALLER("smaller"), XXSMALL("xx-small"), XSMALL("x-small"), SMALL("small"), MEDIUM("medium"), LARGE("large"),
77 XLARGE("x-large"), XXLARGE("xx-large"), LARGER("larger");
78
79 private final String size;
80
81 private FontSize(final String size) {
82 this.size = size;
83 }
84
85 public String getFontSize() {
86 return size;
87 }
88
89 public static FontSize getFontSize(final String size) {
90 for (final FontSize fontSize : values()) {
91 if (fontSize.size.equals(size)) {
92 return fontSize;
93 }
94 }
95 return SMALL;
96 }
97
98 public FontSize larger() {
99 return this.ordinal() < XXLARGE.ordinal() ? FontSize.values()[this.ordinal() + 1] : this;
100 }
101 }
102
103 private final String font;
104 private final String fontSize;
105 private final String headerSize;
106
107 private HtmlLayout(final boolean locationInfo, final String title, final String contentType, final Charset charset,
108 final String font, final String fontSize, final String headerSize) {
109 super(charset);
110 this.locationInfo = locationInfo;
111 this.title = title;
112 this.contentType = addCharsetToContentType(contentType);
113 this.font = font;
114 this.fontSize = fontSize;
115 this.headerSize = headerSize;
116 }
117
118 private String addCharsetToContentType(final String contentType) {
119 if (contentType == null) {
120 return DEFAULT_CONTENT_TYPE + "; charset=" + getCharset();
121 }
122 return contentType.contains("charset") ? contentType : contentType + "; charset=" + getCharset();
123 }
124
125
126
127
128
129
130
131 @Override
132 public String toSerializable(final LogEvent event) {
133 final StringBuilder sbuf = new StringBuilder(BUF_SIZE);
134
135 sbuf.append(Constants.LINE_SEPARATOR).append("<tr>").append(Constants.LINE_SEPARATOR);
136
137 sbuf.append("<td>");
138 sbuf.append(event.getTimeMillis() - jvmStartTime);
139 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
140
141 final String escapedThread = Transform.escapeHtmlTags(event.getThreadName());
142 sbuf.append("<td title=\"").append(escapedThread).append(" thread\">");
143 sbuf.append(escapedThread);
144 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
145
146 sbuf.append("<td title=\"Level\">");
147 if (event.getLevel().equals(Level.DEBUG)) {
148 sbuf.append("<font color=\"#339933\">");
149 sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel())));
150 sbuf.append("</font>");
151 } else if (event.getLevel().isMoreSpecificThan(Level.WARN)) {
152 sbuf.append("<font color=\"#993300\"><strong>");
153 sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel())));
154 sbuf.append("</strong></font>");
155 } else {
156 sbuf.append(Transform.escapeHtmlTags(String.valueOf(event.getLevel())));
157 }
158 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
159
160 String escapedLogger = Transform.escapeHtmlTags(event.getLoggerName());
161 if (escapedLogger.isEmpty()) {
162 escapedLogger = "root";
163 }
164 sbuf.append("<td title=\"").append(escapedLogger).append(" logger\">");
165 sbuf.append(escapedLogger);
166 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
167
168 if (locationInfo) {
169 final StackTraceElement element = event.getSource();
170 sbuf.append("<td>");
171 sbuf.append(Transform.escapeHtmlTags(element.getFileName()));
172 sbuf.append(':');
173 sbuf.append(element.getLineNumber());
174 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
175 }
176
177 sbuf.append("<td title=\"Message\">");
178 sbuf.append(Transform.escapeHtmlTags(event.getMessage().getFormattedMessage()).replaceAll(REGEXP, "<br />"));
179 sbuf.append("</td>").append(Constants.LINE_SEPARATOR);
180 sbuf.append("</tr>").append(Constants.LINE_SEPARATOR);
181
182 if (event.getContextStack() != null && !event.getContextStack().isEmpty()) {
183 sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : ").append(fontSize);
184 sbuf.append(";\" colspan=\"6\" ");
185 sbuf.append("title=\"Nested Diagnostic Context\">");
186 sbuf.append("NDC: ").append(Transform.escapeHtmlTags(event.getContextStack().toString()));
187 sbuf.append("</td></tr>").append(Constants.LINE_SEPARATOR);
188 }
189
190 if (event.getContextMap() != null && !event.getContextMap().isEmpty()) {
191 sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : ").append(fontSize);
192 sbuf.append(";\" colspan=\"6\" ");
193 sbuf.append("title=\"Mapped Diagnostic Context\">");
194 sbuf.append("MDC: ").append(Transform.escapeHtmlTags(event.getContextMap().toString()));
195 sbuf.append("</td></tr>").append(Constants.LINE_SEPARATOR);
196 }
197
198 final Throwable throwable = event.getThrown();
199 if (throwable != null) {
200 sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : ").append(fontSize);
201 sbuf.append(";\" colspan=\"6\">");
202 appendThrowableAsHtml(throwable, sbuf);
203 sbuf.append("</td></tr>").append(Constants.LINE_SEPARATOR);
204 }
205
206 return sbuf.toString();
207 }
208
209 @Override
210
211
212
213 public String getContentType() {
214 return contentType;
215 }
216
217 private void appendThrowableAsHtml(final Throwable throwable, final StringBuilder sbuf) {
218 final StringWriter sw = new StringWriter();
219 final PrintWriter pw = new PrintWriter(sw);
220 try {
221 throwable.printStackTrace(pw);
222 } catch (final RuntimeException ex) {
223
224 }
225 pw.flush();
226 final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
227 final ArrayList<String> lines = new ArrayList<String>();
228 try {
229 String line = reader.readLine();
230 while (line != null) {
231 lines.add(line);
232 line = reader.readLine();
233 }
234 } catch (final IOException ex) {
235 if (ex instanceof InterruptedIOException) {
236 Thread.currentThread().interrupt();
237 }
238 lines.add(ex.toString());
239 }
240 boolean first = true;
241 for (final String line : lines) {
242 if (!first) {
243 sbuf.append(TRACE_PREFIX);
244 } else {
245 first = false;
246 }
247 sbuf.append(Transform.escapeHtmlTags(line));
248 sbuf.append(Constants.LINE_SEPARATOR);
249 }
250 }
251
252
253
254
255
256 @Override
257 public byte[] getHeader() {
258 final StringBuilder sbuf = new StringBuilder();
259 sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" ");
260 sbuf.append("\"http://www.w3.org/TR/html4/loose.dtd\">");
261 sbuf.append(Constants.LINE_SEPARATOR);
262 sbuf.append("<html>").append(Constants.LINE_SEPARATOR);
263 sbuf.append("<head>").append(Constants.LINE_SEPARATOR);
264 sbuf.append("<meta charset=\"").append(getCharset()).append("\"/>").append(Constants.LINE_SEPARATOR);
265 sbuf.append("<title>").append(title).append("</title>").append(Constants.LINE_SEPARATOR);
266 sbuf.append("<style type=\"text/css\">").append(Constants.LINE_SEPARATOR);
267 sbuf.append("<!--").append(Constants.LINE_SEPARATOR);
268 sbuf.append("body, table {font-family:").append(font).append("; font-size: ");
269 sbuf.append(headerSize).append(";}").append(Constants.LINE_SEPARATOR);
270 sbuf.append("th {background: #336699; color: #FFFFFF; text-align: left;}").append(Constants.LINE_SEPARATOR);
271 sbuf.append("-->").append(Constants.LINE_SEPARATOR);
272 sbuf.append("</style>").append(Constants.LINE_SEPARATOR);
273 sbuf.append("</head>").append(Constants.LINE_SEPARATOR);
274 sbuf.append("<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">").append(Constants.LINE_SEPARATOR);
275 sbuf.append("<hr size=\"1\" noshade=\"noshade\">").append(Constants.LINE_SEPARATOR);
276 sbuf.append("Log session start time " + new java.util.Date() + "<br>").append(Constants.LINE_SEPARATOR);
277 sbuf.append("<br>").append(Constants.LINE_SEPARATOR);
278 sbuf.append(
279 "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">");
280 sbuf.append(Constants.LINE_SEPARATOR);
281 sbuf.append("<tr>").append(Constants.LINE_SEPARATOR);
282 sbuf.append("<th>Time</th>").append(Constants.LINE_SEPARATOR);
283 sbuf.append("<th>Thread</th>").append(Constants.LINE_SEPARATOR);
284 sbuf.append("<th>Level</th>").append(Constants.LINE_SEPARATOR);
285 sbuf.append("<th>Logger</th>").append(Constants.LINE_SEPARATOR);
286 if (locationInfo) {
287 sbuf.append("<th>File:Line</th>").append(Constants.LINE_SEPARATOR);
288 }
289 sbuf.append("<th>Message</th>").append(Constants.LINE_SEPARATOR);
290 sbuf.append("</tr>").append(Constants.LINE_SEPARATOR);
291 return sbuf.toString().getBytes(getCharset());
292 }
293
294
295
296
297
298 @Override
299 public byte[] getFooter() {
300 final StringBuilder sbuf = new StringBuilder();
301 sbuf.append("</table>").append(Constants.LINE_SEPARATOR);
302 sbuf.append("<br>").append(Constants.LINE_SEPARATOR);
303 sbuf.append("</body></html>");
304 return getBytes(sbuf.toString());
305 }
306
307
308
309
310
311
312
313
314
315
316
317 @PluginFactory
318 public static HtmlLayout createLayout(
319 @PluginAttribute(value = "locationInfo", defaultBoolean = false) final boolean locationInfo,
320 @PluginAttribute(value = "title", defaultString = DEFAULT_TITLE) final String title,
321 @PluginAttribute("contentType") String contentType,
322 @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charset,
323 @PluginAttribute("fontSize") String fontSize,
324 @PluginAttribute(value = "fontName", defaultString = DEFAULT_FONT_FAMILY) final String font) {
325 final FontSize fs = FontSize.getFontSize(fontSize);
326 fontSize = fs.getFontSize();
327 final String headerSize = fs.larger().getFontSize();
328 if (contentType == null) {
329 contentType = DEFAULT_CONTENT_TYPE + "; charset=" + charset;
330 }
331 return new HtmlLayout(locationInfo, title, contentType, charset, font, fontSize, headerSize);
332 }
333
334
335
336
337
338
339 public static HtmlLayout createDefaultLayout() {
340 return newBuilder().build();
341 }
342
343 @PluginBuilderFactory
344 public static Builder newBuilder() {
345 return new Builder();
346 }
347
348 public static class Builder implements org.apache.logging.log4j.core.util.Builder<HtmlLayout> {
349
350 @PluginBuilderAttribute
351 private boolean locationInfo = false;
352
353 @PluginBuilderAttribute
354 private String title = DEFAULT_TITLE;
355
356 @PluginBuilderAttribute
357 private String contentType = null;
358
359 @PluginBuilderAttribute
360 private Charset charset = Charsets.UTF_8;
361
362 @PluginBuilderAttribute
363 private FontSize fontSize = FontSize.SMALL;
364
365 @PluginBuilderAttribute
366 private String fontName = DEFAULT_FONT_FAMILY;
367
368 private Builder() {
369 }
370
371 public Builder withLocationInfo(final boolean locationInfo) {
372 this.locationInfo = locationInfo;
373 return this;
374 }
375
376 public Builder withTitle(final String title) {
377 this.title = title;
378 return this;
379 }
380
381 public Builder withContentType(final String contentType) {
382 this.contentType = contentType;
383 return this;
384 }
385
386 public Builder withCharset(final Charset charset) {
387 this.charset = charset;
388 return this;
389 }
390
391 public Builder withFontSize(final FontSize fontSize) {
392 this.fontSize = fontSize;
393 return this;
394 }
395
396 public Builder withFontName(final String fontName) {
397 this.fontName = fontName;
398 return this;
399 }
400
401 @Override
402 public HtmlLayout build() {
403
404 if (contentType == null) {
405 contentType = DEFAULT_CONTENT_TYPE + "; charset=" + charset;
406 }
407 return new HtmlLayout(locationInfo, title, contentType, charset, fontName, fontSize.getFontSize(),
408 fontSize.larger().getFontSize());
409 }
410 }
411 }