Java 为什么为 Log4J 配置选择 XML 而不是属性文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1256835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:31:00  来源:igfitidea点击:

Why chose XML over properties files for Log4J configuration?

javaxmllogginglog4j

提问by otto.poellath

Are there any reasons to use XML over properties files for Log4J configuration?

是否有任何理由在 Log4J 配置中使用 XML over properties 文件?

采纳答案by Rich Seller

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. Log4j looks for a file named log4j.xml and then for a file named log4j.properties. Both must be placed in the src folder.

The property file is less verbose than an XML file. The XML requires the log4j.dtd to be placed in the source folder as well. The XML requires a dom4j.jar which might not be included in older Java versions.

The properties file does not support some advanced configuration options like Filters, custom ErrorHandlers and a special type of appenders, i.e. AsyncAppender. ErrorHandlers defines how errors in log4j itself are handled, for example badly configured appenders. 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. This allows to split log messages across different logfiles. One for DEBUGGING messages, another for warnings, ...

The property appender only supports a minimum level. If you set it do INFO, you will receive WARN, ERROR and FATAL messages as well.

属性可以由属性文件或 XML 文件定义。Log4j 查找名为 log4j.xml 的文件,然后查找名为 log4j.properties 的文件。两者都必须放在 src 文件夹中

属性文件不如 XML 文件详细。XML 还要求将 log4j.dtd 放置在源文件夹中。XML 需要一个 dom4j.jar,旧的 Java 版本中可能不包含该文件。

属性文件不支持一些高级配置选项,如过滤器、自定义 ErrorHandler 和特殊类型的附加程序,即 AsyncAppender。ErrorHandlers 定义了如何处理 log4j 本身中的错误,例如错误配置的 appender。过滤器更有趣。从可用的过滤器来看,我认为属性文件确实缺少级别范围过滤器。

此过滤器允许定义 [n] 附加程序应接收从 Level INFO 到 WARN 的日志消息。这允许在不同的日志文件中拆分日志消息。一个用于调试消息,另一个用于警告,......

属性 appender 只支持最低级别。如果您将其设置为 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. 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.

为了解决对我的原始答案的评论:斜体是我的重点。出于本教程的目的,作者选择掩盖或无意中忽略了属性或 xml 只需要在类路径上而不是在 src 文件夹中。将它们添加到类路径的一种简单方法是将它们添加到 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. I consider that the rest of the quote is relevant and useful to those looking to make an informed choice.

这些都与提出的问题或答案的意图没有直接关系,即讨论使用 xml 文件配置 log4j 的优点或其他方式。我认为引用的其余部分对于那些希望做出明智选择的人来说是相关且有用的。

回答by Steve B.

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 的许多其他改进。

回答by ZZ Coder

log4j is gradually moving to XML so properties is the legacy format.

log4j 正在逐渐转向 XML,因此属性是遗留格式。

Some new features can only be configured in XML. I was forced to switch to XMl because I need to use TimeBasedRollingPolicy.

一些新功能只能在 XML 中配置。我被迫切换到 XMl,因为我需要使用 TimeBasedRollingPolicy。

However, XML is so verbose. I still use properties whenever I can.

但是,XML 太冗长了。我仍然尽可能使用属性。

回答by Ran

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.
So, if you plan on using perf4j (I'd recommend) then an xml format is required.

Perf4j ( http://perf4j.codehaus.org) 是一个非常好的性能监控系统,它与 log4j 一起配置并与其协同工作,并且需要 log4j.xml 文件。
因此,如果您计划使用 perf4j(我推荐),则需要 xml 格式。