如何让XML在logback的HTMLLayout中转义?
我正在使用 logback(使用 slf4j)进行日志记录,并且我有许多 XML 内容要记录在文本文件和 HTML 文件(使用 HTMLLayout)中。然而,logback 只是将原始 XML 插入到 中。 HTMLLayout 的标签,没有任何转义或
加工。
这是我的 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>
以下是我得到的:
<td class="Message">(DemoCall) parsing response failed. Details:
<call><action>getmessage</action></call>
</td>
我所期望的:
<td class="Message">(DemoCall) parsing response failed. Details:
<call><action>getmessage</action></call>
</td>
或者更好地将上述消息用
包装起来。标签。 我需要扩展 HTMLLayout 来存档吗?或者我的工作是为每个日志语句执行 StringEscapeUtils.escapeHTML(msg) (我不会这样做,因为还有一个不需要转义的文件附加程序)。
谢谢!
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 HTMLLayout, 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。看来我必须自己做:从 logback 扩展 HTMLLayout 并处理那里的转义。
请检查代码的第 61 行 http://gist.github.com/284628
编辑
当然,您必须使用新的扩展 HTMLLyout 然后替换 logback.xml 中的原始 HTMLLyout,其中
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=...>