Why are log files commonly flat?

2019-07-23 16:23发布

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条回答
【Aperson】
2楼-- · 2019-07-23 17:02

"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.

查看更多
登录 后发表回答