简体   繁体   中英

How to use one log4j2 configuration file for runtime in IDE and another log4j2 configuration file for packaging/deployment?

Using Eclipse 06-2021, Maven, log4j2.

I am using log4j2 framework for logging in my application. I created configuration in resources folder in a log4j2.properties file.

I defined there some loggers and their levels:

...
logger.filelogconvert.appenderRef = FileLogConvert
logger.fileloggeneral.appenderRef = FileLogGeneral
rootLogger.appenderRef.console.ref = FileLogGeneral

logger.fileloggeneral.level = warn
rootLogger.level = warn

However i need to rewrite this file when i want to deploy application, because i need to change levels from info to warn or change appender/logger references.

Using log4j2-test.properties file: https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

...
logger.filelogconvert.appenderRef = Console
logger.fileloggeneral.appenderRef = Console
rootLogger.appenderRef.console.ref = Console

logger.fileloggeneral.level = info
rootLogger.level = info

Then exlude it in pom.xml:

<build>
    ...
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>log4j2-test.properties</exclude>
            </excludes>
        </resource>
    </resources>
    ...
</build>

There surely are several other ways how to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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