How to make XML get escaped in HTMLLayout of logback?
I'm using logback (with slf4j) to do the logging, and I've got many XML content to be logged in both text files and HTML files (with HTMLLayout). However, logback just inserts the raw XML in the <TD> tags for the HTML开发者_运维问答Layout, without any escaping or <pre> processing.
Here is the snippet of my logback.xml:
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${DIR_ALL}/%d{yyyy-MM-dd}.%i.html</FileNamePattern>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>500KB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%logger{1}%msg</pattern>
<cssBuilder class="ch.qos.logback.classic.html.UrlCssBuilder">
<url>${CSS_HREF}</url>
</cssBuilder>
<title>Logs (ALL)</title>
</layout>
And the following is what I got:
<td class="Message">(DemoCall) parsing response failed. Details:
<call><action>getmessage</action></call>
</td>
What I'm expecting:
<td class="Message">(DemoCall) parsing response failed. Details:
<call><action>getmessage</action></call>
</td>
Or better wrap the above message with a <pre> tag. Do I need to extend the HTMLLayout to archive that? Or is it my job to do a StringEscapeUtils.escapeHTML(msg) for each log statement (I'm not going to do that, since there also is a file appender for which the escaping is not needed).
Thanks!
OK. It seems that I must do it myself: extend the HTMLLayout from logback and handle the escaping thing there.
Please check line 61 of the code at http://gist.github.com/284628
EDIT
Of course you must use the new extended HTMLLyout then to replace the original one in logback.xml where <layout class=...>
精彩评论