使用单一的log4j2 xml文件配置log4j2和log4j[英] Configuring log4j2 and log4j using a single log4j2 xml file

本文是小编为大家收集整理的关于使用单一的log4j2 xml文件配置log4j2和log4j的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经将应用程序迁移到log4j 2,并且已经通过log4j2.xml

进行了配置

但是,我正在使用的某些库取决于log4j 1.如果我使用:

运行该应用程序
-Dlog4j.configurationFile=path/to/log4j2.xml

log4j 1抱怨没有找到配置文件.我正在使用log4j 2,log4j-1.2-api-2.0-rc1.jar提供的log4j 1.x桥.是否可以使用单个log4j2.xml?

配置两者

我尝试过的一种替代方案是一起配置log4j和log4j2:

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

我担心的是我的记录配置文件和输出的碎片.我还担心log4j.xml和log4j2.xml之间可能发生冲突.例如logfile error.log被配置为在log4j 1中使用fileappender和log4j 2中的rollingfileappender.

有建议吗?

[注意]

这是我看到的错误:

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.

我正在使用的log4j 2的版本是log4j 2.0 rc1.

[答案]

似乎是ActiveMQ-5.8.0.Jar与Log4j捆绑在一起1.解决方案只是在ActiveMQ之前加载Log4j 1.x桥.

.

推荐答案

我建议使用log4j2分发中包含的log4j-1.2适配器.这样,编码为log4j-1.2 API的任何库将与Log4J2一起使用,而无需任何代码更改.

您的类路径应包括:

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

您的classpath应该不包括:

  • log4j-1.2.x.jar
  • log4j.properties或log4j.xml(无论如何将被log4j2忽略)

另请参阅 2.x/faq.html#what_jars

其他推荐答案

在使用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>

有关此信息的更多信息常见问题

本文地址:https://www.itbaoku.cn/post/1574744.html

问题描述

I've migrated my application to log4j 2, and I've configured it via log4j2.xml

However, some of the libraries I'm using depend on log4j 1. If I run the application using:

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

log4j 1 complains about not finding a configuration file. I'm using the log4j 1.x bridge provided by log4j 2, log4j-1.2-api-2.0-rc1.jar. Is it possible to configure both using a single log4j2.xml?

An alternative I've tried is configuring both log4j and log4j2 together:

-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. e.g. the logfile error.log is configured to use a FileAppender in log4j 1 and a RollingFileAppender in log4j 2.

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.

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

推荐答案

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution. That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes.

Your classpath should include:

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

Your classpath should not include:

  • log4j-1.2.x.jar
  • log4j.properties or log4j.xml (these will be ignored by log4j2 anyway)

See also 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:

<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