为什么在 Log4J 配置中选择 XML 而不是属性文件? [英] Why chose XML over properties files for Log4J configuration?

查看:18
本文介绍了为什么在 Log4J 配置中选择 XML 而不是属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

有一个有趣的讨论本博客中两者的优点.以下部分引用自该博客:

There's an interesting discussion on the merits of both in this blog. The section below is a quote from that blog:

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

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 文件那么冗长.XML 还要求将 log4j.dtd 放置在源文件夹中.XML 需要 dom4j.jar,旧的 Java 版本中可能不包含该文件.

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.

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

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.

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

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

属性附加器仅支持最低级别.如果您将其设置为 INFO,您还将收到 WARN、ERROR 和 FATAL 消息.

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 只需要在类路径上而不是在 src 文件夹中.将它们添加到类路径的一种简单方法是将它们添加到 src 文件夹,因此对于本教程而言,这显然已经足够了.


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 文件配置 log4j 的优点或其他方式.我认为引用的其余部分对于那些希望做出明智选择的人来说是相关且有用的.

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.

这篇关于为什么在 Log4J 配置中选择 XML 而不是属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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