简体   繁体   English

为什么选择XML over属性文件进行Log4J配置?

[英]Why chose XML over properties files for Log4J configuration?

是否有任何理由在Log4J配置的属性文件上使用XML?

There's an interesting discussion on the merits of both in this blog . 关于这两个博客优点,有一个有趣的讨论。 The section below is a quote from that blog: 以下部分是该博客的引用:

Properties can be defined by a properties file or by an XML file. 属性可以由属性文件或XML文件定义。 Log4j looks for a file named log4j.xml and then for a file named log4j.properties. Log4j查找名为log4j.xml的文件,然后查找名为log4j.properties的文件。 Both must be placed in the src folder . 两者都必须放在src文件夹中

The property file is less verbose than an XML file. 属性文件比XML文件简洁。 The XML requires the log4j.dtd to be placed in the source folder as well. XML要求log4j.dtd也放在源文件夹中。 The XML requires a dom4j.jar which might not be included in older Java versions. XML需要dom4j.jar,它可能不包含在较旧的Java版本中。

The properties file does not support some advanced configuration options like Filters, custom ErrorHandlers and a special type of appenders, ie AsyncAppender. 属性文件不支持某些高级配置选项,如过滤器,自定义ErrorHandler和特殊类型的appender,即AsyncAppender。 ErrorHandlers defines how errors in log4j itself are handled, for example badly configured appenders. ErrorHandlers定义了如何处理log4j本身的错误,例如配置错误的appender。 Filters are more interesting. 过滤器更有趣。 From the available filters, I think that the level range filter is really missing for property files. 从可用的过滤器,我认为属性文件实际上缺少级别范围过滤器。

This filter allows to define that a[n] appender should receive log messages from Level INFO to WARN. 此过滤器允许定义[n] appender应接收从Level INFO到WARN的日志消息。 This allows to split log messages across different logfiles. 这允许跨不同的日志文件分割日志消息。 One for DEBUGGING messages, another for warnings, ... 一个用于DEBUGGING消息,另一个用于警告,......

The property appender only supports a minimum level. 属性appender仅支持最低级别。 If you set it do INFO, you will receive WARN, ERROR and FATAL messages as well. 如果将其设置为INFO,您将收到WARN,ERROR和FATAL消息。


To address the comments on my original answer: The italics are my emphasis. 解决我原来答案的评论:斜体是我的重点。 For the purposes of the tutorial the author has chosen to gloss over, or unintentionally omitted that the properties or xml need only be on the classpath rather than needing to be in the src folder. 出于本教程的目的,作者选择掩盖或无意中省略了属性或xml只需要在类路径上而不是需要在src文件夹中。 A simple way to add them to the classpath is to add them to the src folder, so for the purpose of the tutorial that was obviously deemed sufficient. 将它们添加到类路径的一种简单方法是将它们添加到src文件夹中,因此出于本教程的目的,这显然是足够的。

None of this is directly relevant to the question asked or the intention of the answer, which is to discuss the merits or otherwise of using xml files to configure log4j. 这些都与提出的问题或答案的意图没有直接关系,这是讨论使用xml文件配置log4j的优点或其他方面。 I consider that the rest of the quote is relevant and useful to those looking to make an informed choice. 我认为引用的其余部分对于那些希望做出明智选择的人来说是相关且有用的。

log4j is gradually moving to XML so properties is the legacy format. log4j正逐渐转向XML,因此属性是传统格式。

Some new features can only be configured in XML. 某些新功能只能以XML格式配置。 I was forced to switch to XMl because I need to use TimeBasedRollingPolicy. 我被迫切换到XMl,因为我需要使用TimeBasedRollingPolicy。

However, XML is so verbose. 但是,XML非常冗长。 I still use properties whenever I can. 我还是尽可能地使用属性。

Well, one thing you can only do in an xml configuration is to set up a logger to use buffering (using the org.apache.log4j.AsyncAppender). 好吧,你只能在xml配置中做的一件事是设置一个记录器来使用缓冲(使用org.apache.log4j.AsyncAppender)。

If you need more functionality, however, you might also want to look at logback, which contains a number of other improvements over log4j. 但是,如果您需要更多功能,您可能还需要查看logback,其中包含对log4j的许多其他改进。

Perf4j ( http://perf4j.codehaus.org ) is a very nice performance monitoring system, which is configured alongside log4j and works in conjunction to it and requires log4j.xml files. Perf4j( http://perf4j.codehaus.org )是一个非常好的性能监视系统,它与log4j一起配置并与它一起工作并需要log4j.xml文件。
So, if you plan on using perf4j (I'd recommend) then an xml format is required. 因此,如果您计划使用perf4j(我建议),则需要xml格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM