Spring Profiles, different Log4j2 configs

As an alternative to using Spring profiles (which requires you to explicitly set which profiles are active), you can use a Maven build profile to switch between log4j2 configuration files.

application.yml

logging:
    config: classpath:${log4j2.config}

pom.xml

<project>
    <properties>
        <log4j2.config>log4j2.xml</log4j2.config>
    </properties>
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <log4j2.config>log4j2-local.xml</log4j2.config>
            </properties>
        </profile>
    </profiles>
</project>

In this way, a default log4j config file (log4j2.xml) can be used for normal builds.

And a second config file (log4j2-local.xml) can be used for local development/testing whenever the project is built with the local build profile (e.g. mvn package -Plocal).


Here is the solution that worked for me with spring boot 2 and log4j2.xml Make sure you have files like application-local.properties, application-dev.properties for different envs and profiles.

Never keep log4j2.xml in the src/main/resources folder instead it should be kept under profile specific folders created under src/main/resources as src/main/resources/local src/main/resources/dev etc... then make entries like logging.config: classpath:local/log4j2.xml in application-local.properties and logging.config: classpath:dev/log4j2.xml in application-dev.properties

also keep log4j2.xml in each of these folders with the same file name log4j2.xml and once again never keep one under src/main/resources since the application will pick it by default which we do not want. Have different configuration in each if these different xml specific to your environments and it should work.. Also run the spring boot with the profile argument supplied..

env specific properties entry

run application for the env or profile needed

Then you can modify log file paths based on the environments as below...

    <RollingFile name="RollingFileAppender" fileName="c:\\logs\\logs_test\\og4j2-demo.log"  filePattern="c:\\logs\\logs_test\\log4j2-demo-%d{yyyy-MM-dd}-%i.log"> 

On my side, I am using properties file instead of a yaml one. I wanted two log files: one which logs everything to the console, and another one which logs in a file. So I made two log4j2 configuration files: log4j2-dev.xml and log4j2-file.xml.

I use two Spring profile: the default one and one named "dev". To switch the log4j2 configuration file, I created a file application.properties containing:

spring.profiles.active=
logging.config=classpath:log4j2-file.xml

And I have another properties file, application-dev.properties, which is activated automatically when Spring detects the "dev" profile. It contains:

logging.config=classpath:log4j2-dev.xml

When I want to use the log4j2-dev.xml configuration, I just add "dev" as value of "spring.profiles.active=" in application.properties.

You can take a look to the Feiyu Zhou's answer at this page. He present a solution using a Yaml configuration file: How to define log4j2 path by application.properties?

Of course, you could always remove the attribute logging.config of application.properties and rename log4j2-file.xml in log4j2.xml. It will be loaded by default by Log4j2 without the need to be triggered by a Spring profile