Why are log files commonly flat?

冷暖自知 提交于 2020-01-06 03:03:06

问题


Are log files not meant to be read by machines but by users only? I wonder if there are file appenders for any logging framework that write their output to XML.


回答1:


"Logging to XML" is quite a general requirement, because there is no such thing as the standard log file format. But since XML files are text files, which logging frameworks can write, and since many of those frameworks allow configuring log line format, I see no problem in defining your log output with XML tags of choice.

For log4j, it might be something like this:

log4j.appender.A1.layout.ConversionPattern=<line>%n<date>%d</date>%n<threadName>%t</threadName>%n<level>%p</level>%n<logger>%c</logger>%n<text>%m</text>%n</line>%n

yielding example output:

<line>
<date>2011-08-28 08:27:33,727</date>
<threadName>main</threadName>
<level>INFO</level>
<logger>com.log4jeval.Main</logger>
<text>Entering application.</text>
</line>

This looks quite like XML, doesn't it? It will lack XML preamble, though, so technically it won't be valid. If it's critical that it is, I recommend writing a custom appender extending org.apache.log4j.FileAppender (or any of its subclasses) that would handle any additional opening/closing text in every log file.

The problem with writing logs to XML, that does not exist in plain text files, is that you have to enforce that not one possible log statement would print XML forbidden characters, otherwise you'll end up with a non well-formed XML. That's hard to achieve without writing a custom appender — see org.apache.log4j.HTMLAppender sources for example.



来源:https://stackoverflow.com/questions/7149409/why-are-log-files-commonly-flat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!