简体   繁体   English

使用单个 log4j2 xml 文件配置 log4j2 和 log4j

[英]Configuring log4j2 and log4j using a single log4j2 xml file

I've migrated my application to log4j 2, and I've configured it via log4j2.xml我已将我的应用程序迁移到 log4j 2,并通过 log4j2.xml 对其进行了配置

However, some of the libraries I'm using depend on log4j 1. If I run the application using:但是,我使用的一些库依赖于 log4j 1. 如果我使用以下命令运行应用程序:

-Dlog4j.configurationFile=path/to/log4j2.xml

log4j 1 complains about not finding a configuration file. log4j 1 抱怨找不到配置文件。 I'm using the log4j 1.x bridge provided by log4j 2, log4j-1.2-api-2.0-rc1.jar.我正在使用 log4j 2 提供的 log4j 1.x 桥,log4j-1.2-api-2.0-rc1.jar。 Is it possible to configure both using a single log4j2.xml?是否可以使用单个 log4j2.xml 配置两者?

An alternative I've tried is configuring both log4j and log4j2 together:我尝试过的另一种方法是同时配置 log4j 和 log4j2:

-Dlog4j.configurationFile=path/to/log4j2.xml -Dlog4j.configuration=path/to/log4j.xml

My concern is fragmentation of my logging configuration files and output.我担心的是我的日志配置文件和输出的碎片。 I'm also concerned about possible conflicts between log4j.xml and log4j2.xml.我还担心 log4j.xml 和 log4j2.xml 之间可能存在的冲突。 eg the logfile error.log is configured to use a FileAppender in log4j 1 and a RollingFileAppender in log4j 2.例如,日志文件 error.log 被配置为在 log4j 1 中使用 FileAppender,在 log4j 2 中使用 RollingFileAppender。

Any advice?有什么建议吗?

[note] [笔记]

This is the error I'm seeing:这是我看到的错误:

log4j:WARN No appenders could be found for logger (org.apache.activemq.util.ThreadPoolUtils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

The version of log4j 2 I'm using is log4j 2.0 rc1.我使用的 log4j 2 版本是 log4j 2.0 rc1。

[answer] [回答]

Seems like activemq-5.8.0.jar was bundled with log4j 1. The solution was simply to load the log4j 1.x bridge before activemq.似乎 activemq-5.8.0.jar 与 log4j 1 捆绑在一起。解决方案只是在 activemq 之前加载 log4j 1.x 桥。

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution.我建议使用 log4j2 发行版中包含的 log4j-1.2 适配器。 That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes.这样,任何编码到 log4j-1.2 API 的库都可以与 log4j2 一起使用,而无需更改任何代码。

Your classpath should include:你的类路径应该包括:

  • log4j-api-2.6.1.jar log4j-api-2.6.1.jar
  • log4j-core-2.6.1.jar log4j-core-2.6.1.jar
  • log4j-1.2-api-2.6.1.jar log4j-1.2-api-2.6.1.jar
  • log4j2.xml log4j2.xml

Your classpath should not include:你的类路径应该包括:

  • log4j-1.2.x.jar log4j-1.2.x.jar
  • log4j.properties or log4j.xml (these will be ignored by log4j2 anyway) log4j.properties 或 log4j.xml(无论如何这些都会被 log4j2 忽略)

See also http://logging.apache.org/log4j/2.x/faq.html#which_jars另见http://logging.apache.org/log4j/2.x/faq.html#which_jars

In a maven project using log4j2, it is possible to exclude log4j from modules that use it, in the pom, in order to enable log4j2 to take over:在使用 log4j2 的 maven 项目中,可以从使用它的模块中排除 log4j,在 pom 中,以便启用 log4j2 接管:

<dependencies>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.4</version>
        <!--force usage of log4j2-->
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>

More info about that in the log4j FAQ log4j 常见问题中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM