Configuring log4j2 and log4j using a single log4j2 xml file

后端 未结 2 1274
甜味超标
甜味超标 2020-12-02 23:14

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 applicatio

相关标签:
2条回答
  • 2020-12-02 23:58

    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

    0 讨论(0)
  • 2020-12-03 00:03

    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

    0 讨论(0)
提交回复
热议问题