log4j.properties vs log4j.xml[英] log4j.properties vs log4j.xml

本文是小编为大家收集整理的关于log4j.properties vs log4j.xml的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我被要求从属性样式配置转移到XML样式.这个过程本身很简单,这并没有给我带来任何麻烦,我只是好奇为什么要问这个问题,而我寻找原因.我发现这篇不错的帖子为什么选择xml而不是log4j configuration xml ?(超过3岁,但仍然可以找到最有用的内容),它指向这个甚至较旧的教程.他们说

属性可以由属性文件或XML文件定义.

log4j寻找名为log4j.xml的文件,然后寻找名为log4j.properties的文件.两者都必须放在SRC文件夹中.

我检查了一下,与声明相反,发现Log4j首先查找属性文件,第二次是XML.实际上,如果我将两个都放在同一文件夹中,则根据属性文件将输出格式化.那么,我误解了这一说法吗?这简直是​​错误的吗?该声明是为log4j 1.2.14 做出的,而我在log4j 1.2.17 时,log4j团队是否有可能更改优先级?我找不到任何相关文档,但这将是一个奇怪的趋势反转,因为XML看起来是配置log4j

的首选方法

推荐答案

请看一下Javadoc. propertyconfiguratorConfiguratorClassClassClass (log4j.properties)/p>

PropertyConfigurator不处理高级配置 Domconfigurator支持的功能,例如支持自定义 错误手机,嵌套的附录,例如异步人,等等.

所以 domconfigurator (log4j.xml)提供高级选项.

除此之外/xml/doc-files/log4j.dtd"> log4j.dtd .

如果您不使用高级功能,则不必从属性更改为XML文件.

您真正应该考虑的是从log4j变为 log4j 2 beta slf4j . Log4J的开发已停止,IT的创始人(@CEKI)发明了SLF4J.

其他推荐答案

按照 frvabe的答案,属性configurator类无法处理XML可以支持的高级功能.<<<<<<<<<<<<<<<<<<<

值得注意的是,更有用的高级选项之一(仅可用的log4j.xml)是能够使用latver -matchfilter和denyAllFilter

在特定日志级别或日志级别上过滤的能力

可以看到一个很好的例子在这里..

本文地址:https://www.itbaoku.cn/post/1574767.html

问题描述

I was asked to move from properties style configuration to xml style. The process itself is straightforward and it is not causing me any trouble, I was simply curious why I got asked this and I looked for the reason. I found this nice post Why chose XML over properties files for Log4J configuration? (more than 3 years old but still the most informative I could find), which was pointing pointing to this even older tutorial. They state

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.

I checked it out and, contrary to the statement, found out that Log4j looks first for the properties file and in a second time for xml. In fact, if I keep both in the same folder, the output is formatted according to the properties file. So, am I misunderstood the statement? Is it simply wrong? The statement has been made for log4j 1.2.14 while I am on log4j 1.2.17, is it possible the log4j team changed the priority? I couldn't find any related documentation, but it will be a strange trend inversion since the xml looked to be the preferred way to configure log4j

推荐答案

Have a look at the JavaDoc. The documentation of the PropertyConfiguratorClass (log4j.properties) points out that

The PropertyConfigurator does not handle the advanced configuration features supported by the DOMConfigurator such as support custom ErrorHandlers, nested appenders such as the AsyncAppender, etc.

So the DOMConfigurator (log4j.xml) offers advanced options.

Beside that you can have (at least a simple) validity check of log4j.xml files using the provided log4j.dtd.

If youd do not make use of the advanced features it is needless to change from properties to xml files.

What you really should think about is to change from log4j to log4j 2 beta or even slf4j. Development of log4j has stopped and the founder of it (@Ceki) invented slf4j.

其他推荐答案

As per FrVaBe's answer, the PropertyConfigurator class does not handle the advanced features that XML can support.

It's worth noting one of the more useful advanced options (only available log4j.xml) is the ability to filter on a specific log level or range of log levels using LevelMatchFilter and DenyAllFilter

A good example can be seen here.