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.0-rc1和log4j的示例。属性文件,但是log4j lib总是使用默认配置(日志级别、appender等)运行它。我还尝试将名称改为log4j2。属性和什么也没发生。
3 个解决方案
#1
24
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 2不支持Log4j v1。属性“格式”(但是,由于v2.4, Log4j支持属性格式,但是它的语法与v1格式完全不同)。新的格式是XML、JSON和YAML,请参阅文档(注意:如果您在名为“的文件中使用了这些格式之一”。属性“,它可能令人困惑)。
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.
要指定配置文件的位置,请使用系统属性log4j。配置文件,Log4j类配置工厂,还是别的什么?你看了这本手册页了吗?它解释了:尽管Log4j 2配置语法与Log4j 1不同。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:
所以看起来是遗留的Log4j1。x log4j。propertiesfile不被支持,它必须迁移到v2。x格式。迁移看起来很简单,看看我上面给出的链接的例子。这是一个精华:
Example of Log4j v1.x config file:
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>
Same config file migrated to Log4j v2:
同一配置文件迁移到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>
#2
9
As of version 2.4, Log4J2 does now, again, support .property files. See here in the documentation for property configuration.
在版本2.4中,Log4J2现在再次支持.property文件。请参阅本文中关于属性配置的文档。
Configuration with Properties
配置和性能
As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1. Like the XML and JSON configurations, properties configurations define the configuration in terms of plugins and attributes to the plugins.
在2.4版本中,Log4j现在通过属性文件支持配置。注意,属性语法与Log4j 1中使用的语法不一样。与XML和JSON配置一样,属性配置也定义了插件的配置和插件的属性。
#3
1
Log4j 2 uses a new configuration file format. You need to use XML (default), JSON (with additional libraries), or even YAML (again, libraries). Check out the documentation.
Log4j 2使用了新的配置文件格式。您需要使用XML(默认)、JSON(带有额外的库),或者甚至YAML(同样是库)。查看文档。
#1
24
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 2不支持Log4j v1。属性“格式”(但是,由于v2.4, Log4j支持属性格式,但是它的语法与v1格式完全不同)。新的格式是XML、JSON和YAML,请参阅文档(注意:如果您在名为“的文件中使用了这些格式之一”。属性“,它可能令人困惑)。
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.
要指定配置文件的位置,请使用系统属性log4j。配置文件,Log4j类配置工厂,还是别的什么?你看了这本手册页了吗?它解释了:尽管Log4j 2配置语法与Log4j 1不同。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:
所以看起来是遗留的Log4j1。x log4j。propertiesfile不被支持,它必须迁移到v2。x格式。迁移看起来很简单,看看我上面给出的链接的例子。这是一个精华:
Example of Log4j v1.x config file:
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>
Same config file migrated to Log4j v2:
同一配置文件迁移到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>
#2
9
As of version 2.4, Log4J2 does now, again, support .property files. See here in the documentation for property configuration.
在版本2.4中,Log4J2现在再次支持.property文件。请参阅本文中关于属性配置的文档。
Configuration with Properties
配置和性能
As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1. Like the XML and JSON configurations, properties configurations define the configuration in terms of plugins and attributes to the plugins.
在2.4版本中,Log4j现在通过属性文件支持配置。注意,属性语法与Log4j 1中使用的语法不一样。与XML和JSON配置一样,属性配置也定义了插件的配置和插件的属性。
#3
1
Log4j 2 uses a new configuration file format. You need to use XML (default), JSON (with additional libraries), or even YAML (again, libraries). Check out the documentation.
Log4j 2使用了新的配置文件格式。您需要使用XML(默认)、JSON(带有额外的库),或者甚至YAML(同样是库)。查看文档。