Java Log4j 2 不再支持 log4j.properties 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22485074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:01:00  来源:igfitidea点击:

Log4j 2 doesn't support log4j.properties file anymore?

javalog4jlog4j2

提问by emanuell

I am running an example using log4j 2.0-rc1 and log4j.propertiesfile, but log4j lib always runs it with the default configuration (log level, appender, etc). I also tried changing the name to log4j2.propertiesand nothing happened.

我正在运行一个使用 log4j 2.0-rc1 和log4j.properties文件的示例,但 log4j lib 始终使用默认配置(日志级别、附加程序等)运行它。我也尝试将名称更改为log4j2.properties,但没有任何反应。

采纳答案by xav

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 “.properties” 格式(然而,从 v2.4 开始,Log4j 支持 Property 格式,但其语法与 v1 格式完全不同)。新格式是 XML、JSON 和 YAML,请参阅文档(注意:如果您在名为“.properties”的文件中使用这些格式之一,则可能会造成混淆)。

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.configurationFile、Log4j 类ConfigurationFactory还是其他东西?你读过这个手册页吗?它解释说: 虽然 Log4j 2 配置语法与 Log4j 1.x 不同,但大多数(如果不是全部)相同的功能都可用。

So it seems that a legacy Log4j1.x log4j.propertiesfile 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.properties不支持旧的 Log4j1.x文件,它必须迁移到 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>

回答by Matt

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(同样是库)。查看文档

回答by Hyman Hughes

As of version 2.4, Log4J2 does now, again, support .property files. See here in the documentation for property configuration.

从 version 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.

带属性的配置

从 version 2.4 开始,Log4j 现在支持通过属性文件进行配置。请注意,属性语法与 Log4j 1 中使用的语法不同。与 XML 和 JSON 配置一样,属性配置根据插件和插件的属性定义配置。