Are there any reasons to use XML over properties files for Log4J configuration?
有什么理由在Log4J配置的属性文件上使用XML吗?
4 个解决方案
#1
39
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.
属性可以由属性文件或XML文件定义。Log4j查找一个名为Log4j的文件。然后是一个名为log4j.properties的文件。两者都必须放在src文件夹中。
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.
属性文件比XML文件更详细。XML需要log4j。也可以将dtd放在源文件夹中。XML需要一个dom4j。jar,旧Java版本中可能不包含它。
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.
属性文件不支持一些高级配置选项,如过滤器、自定义错误处理程序和特殊类型的appender(即AsyncAppender)。ErrorHandlers定义了如何处理log4j本身中的错误,例如配置不良的附加程序。过滤器是更有趣。从可用的过滤器中,我认为级别范围过滤器对于属性文件来说确实是缺失的。
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, ...
这个过滤器允许定义一个[n] appender应该接收来自Level INFO的日志消息以发出警告。这允许跨不同的日志文件分割日志消息。一个用于调试消息,另一个用于警告,…
The property appender only supports a minimum level. If you set it do INFO, you will receive WARN, ERROR and FATAL messages as well.
属性appender只支持最小级别。如果您设置它的do INFO,您将收到警告、错误和致命消息。
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的优点或其他方面。我认为其余的报价对于那些想要做出明智选择的人是相关的和有用的。
#2
9
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太冗长了。我仍然尽可能地使用属性。
#3
3
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的许多其他改进。
#4
3
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协同工作,需要log4j。xml文件。因此,如果您打算使用perf4j(我推荐使用它),那么需要一种xml格式。
#1
39
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.
属性可以由属性文件或XML文件定义。Log4j查找一个名为Log4j的文件。然后是一个名为log4j.properties的文件。两者都必须放在src文件夹中。
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.
属性文件比XML文件更详细。XML需要log4j。也可以将dtd放在源文件夹中。XML需要一个dom4j。jar,旧Java版本中可能不包含它。
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.
属性文件不支持一些高级配置选项,如过滤器、自定义错误处理程序和特殊类型的appender(即AsyncAppender)。ErrorHandlers定义了如何处理log4j本身中的错误,例如配置不良的附加程序。过滤器是更有趣。从可用的过滤器中,我认为级别范围过滤器对于属性文件来说确实是缺失的。
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, ...
这个过滤器允许定义一个[n] appender应该接收来自Level INFO的日志消息以发出警告。这允许跨不同的日志文件分割日志消息。一个用于调试消息,另一个用于警告,…
The property appender only supports a minimum level. If you set it do INFO, you will receive WARN, ERROR and FATAL messages as well.
属性appender只支持最小级别。如果您设置它的do INFO,您将收到警告、错误和致命消息。
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的优点或其他方面。我认为其余的报价对于那些想要做出明智选择的人是相关的和有用的。
#2
9
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太冗长了。我仍然尽可能地使用属性。
#3
3
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的许多其他改进。
#4
3
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协同工作,需要log4j。xml文件。因此,如果您打算使用perf4j(我推荐使用它),那么需要一种xml格式。