Log4j 2不再支持log4j.properties文件了吗? [英] Log4j 2 doesn't support log4j.properties file anymore?
问题描述
我正在使用log4j 2.0-rc1和 log4j.properties
文件运行一个示例,但log4j lib始终使用默认配置(日志级别,appender等)运行它。我也尝试将名称更改为 log4j2.properties
,但没有任何反应。
I am running an example using log4j 2.0-rc1 and log4j.properties
file, but log4j lib always runs it with the default configuration (log level, appender, etc). I also tried changing the name to log4j2.properties
and nothing happened.
推荐答案
Log4j 2不再支持Log4j v1.properties格式(但是,从v2.4开始,Log4j支持Property格式,但其语法是与v1格式完全不同)。新格式是XML,JSON和YAML,请参阅文档 (注意:如果您在名为.properties的文件中使用其中一种格式,则可能会造成混淆)。
Log4j 2 doesn't support the Log4j v1 ".properties" format anymore (yet, since v2.4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".properties", it may be confusing).
要指定配置文件的位置,是否使用系统属性 log4j.configurationFile
,Log4j类 ConfigurationFactory
,还是其他什么?
您是否阅读过本手册页?它解释了:
尽管Log4j 2的配置语法与Log4j 1.x的语法不同,但大多数(如果不是全部)相同的功能都可用。
To specify the location of your configuration file, do you use the system property log4j.configurationFile
, the Log4j class ConfigurationFactory
, or something else?
Did you read this manual page? It explains that:
Although the Log4j 2 configuration syntax is different than that of Log4j 1.x, most, if not all, of the same functionality is available.
因此,似乎不支持传统的Log4j1.x log4j.properties
文件,必须将其迁移到v2.x格式。迁移似乎很容易,看看我上面给出的链接中的示例。以下是摘录:
So it seems that a legacy Log4j1.x log4j.properties
file is not supported as is, it must be migrated to v2.x format. The migration seems quite easy though, looking at the example in the link I gave above. Here is an extract:
Log4j v1.x配置文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<category name="org.apache.log4j.xml">
<priority value="info" />
</category>
<Root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</Root>
</log4j:configuration>
将相同的配置文件迁移到Log4j v2:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="info"/>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
这篇关于Log4j 2不再支持log4j.properties文件了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!