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

Viewed 120

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.

1 Answers

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 in jar plugin:

<build>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <excludes>
                <exclude>log4j2-test.properties</exclude>
            </excludes>
        </configuration>
    </plugin>
    ...
</build>

There surely are several other ways how to achieve this.

Related