使用一个log4j2 xml文件配置log4j2和log4j。

时间:2022-01-25 21:50:42

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

我已经将我的应用程序迁移到log4j2,并且我已经通过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. 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?

log4j 1抱怨找不到配置文件。我使用log4j 1。x桥由log4j 2、log4j-1.2-api-2.0-rc1.jar提供。是否可以使用单个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. e.g. the logfile error.log is configured to use a FileAppender in log4j 1 and a RollingFileAppender in log4j 2.

我关心的是日志配置文件和输出的碎片化。我还担心log4j可能的冲突。xml和log4j2.xml。如日志文件的错误。日志被配置为使用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捆绑在一起。解决方案只是加载log4j 1。activemq前x桥。

2 个解决方案

#1


29  

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.

我建议使用log4j-1.2适配器,它包含在log4j2发行版中。这样,任何编码到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。或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

#2


2  

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常见问题的信息。

#1


29  

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.

我建议使用log4j-1.2适配器,它包含在log4j2发行版中。这样,任何编码到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。或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

#2


2  

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常见问题的信息。