Created
January 23, 2010 14:45
-
-
Save xhh/284628 to your computer and use it in GitHub Desktop.
Fix html-not-escaped bug with logback's HTMLLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR; | |
import org.apache.commons.lang.StringEscapeUtils; | |
import ch.qos.logback.classic.spi.ILoggingEvent; | |
import ch.qos.logback.core.pattern.Converter; | |
/** | |
* Extend the {@link ch.qos.logback.classic.html.HTMLLayout} to escape HTML content.<br/> | |
* TODO remove when the bug if fixed by logback. See http://jira.qos.ch/browse/LBCLASSIC-180 | |
* or http://stackoverflow.com/questions/2069135/how-to-make-xml-get-escaped-in-htmllayout-of-logback | |
* @author xhh | |
*/ | |
public class HTMLLayout extends ch.qos.logback.classic.html.HTMLLayout { | |
@SuppressWarnings("unchecked") | |
public String doLayout(ILoggingEvent event) { | |
StringBuilder buf = new StringBuilder(); | |
startNewTableIfLimitReached(buf); | |
boolean odd = true; | |
if (((counter++) & 1) == 0) { | |
odd = false; | |
} | |
String level = event.getLevel().toString().toLowerCase(); | |
buf.append(LINE_SEPARATOR); | |
buf.append("<tr class=\""); | |
buf.append(level); | |
if (odd) { | |
buf.append(" odd\">"); | |
} else { | |
buf.append(" even\">"); | |
} | |
buf.append(LINE_SEPARATOR); | |
Converter<ILoggingEvent> c = head; | |
while (c != null) { | |
appendEventToBuffer(buf, c, event); | |
c = c.getNext(); | |
} | |
buf.append("</tr>"); | |
buf.append(LINE_SEPARATOR); | |
if (event.getThrowableProxy() != null) { | |
getThrowableRenderer().render(buf, event); | |
} | |
return buf.toString(); | |
} | |
private void appendEventToBuffer(StringBuilder buf, | |
Converter<ILoggingEvent> c, ILoggingEvent event) { | |
buf.append("<td class=\""); | |
buf.append(computeConverterName(c)); | |
buf.append("\">"); | |
/* | |
* the following line is added by xhh to replace the original "buf.append(c.convert(event));" | |
* to escape HTML content | |
*/ | |
buf.append("<pre>").append(StringEscapeUtils.escapeHtml(c.convert(event))).append("</pre>"); | |
buf.append("</td>"); | |
buf.append(LINE_SEPARATOR); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment