为什么日志文件通常是平面的? [英] Why are log files commonly flat?

查看:65
本文介绍了为什么日志文件通常是平面的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日志文件不是要由机器读取而是由用户读取吗?我想知道是否有用于任何日志记录框架的文件追加器,将它们的输出写入XML.

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.

推荐答案

登录到XML"是相当普遍的要求,因为没有标准日志文件格式之类的东西.但是由于XML文件是文本文件,日志框架可以编写,并且其中许多框架都允许配置日志行格式,所以我认为用所选的XML标签定义日志输出没有问题.

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

对于log4j,可能是这样的:

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

产量示例输出:

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

这看起来很像XML,不是吗?但是,它将缺少XML序言,因此从技术上讲它将是无效的.如果这很关键,我建议编写一个扩展org.apache.log4j.FileAppender(或其任何子类)的自定义附加程序,该附加程序将处理每个日志文件中的任何其他打开/关闭文本.

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.

将日志写入XML(纯文本文件中不存在)的问题是,您必须强制没有一个可能的日志语句将打印XML禁止的字符,否则您将结束格式不正确的XML.如果不编写自定义附加程序,则很难实现—.例如,请参见org.apache.log4j.HTMLAppender来源.

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.

这篇关于为什么日志文件通常是平面的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆