How to configure log4j2.xml with different parameters per environment

Asked

Viewed 117 times

2

How to use log4j2 with different parameters per environment in spring boot. I need the log to be generated with a different name for each environment.

1 answer

2

Implement log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="teste"
    packages="">
    <Appenders>
        <RollingFile name="FileAppenderdev" fileName="dev.log"
            filePattern="dev-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>dev - %d [%-6p] %c - %M - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
        <RollingFile name="FileAppendertest" fileName="test.log"
            filePattern="test-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>test - %d [%-6p] %c - %M - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="[email protected]@" />
        </Root>
    </Loggers>
</Configuration>

Define profiles in pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile>dev</spring.profile>
        </properties>
    </profile>
    <profile>
        <id>hom</id>
        <activation>
            <property>
                <name>env</name>
                <value>hom</value>
            </property>
        </activation>
        <properties>
            <spring.profile>test</spring.profile>
        </properties>
    </profile>
</profiles>

Don’t forget the filter!

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Run using

mvn clean install -Dmaven.test.skip=true spring-boot:run -Phom

I hope it helps.

  • I didn’t quite understand the need for the filter in the src/main/resources. And by the way, it’s considered preferable to use -DskipTests when using the surefire.

  • 1

    @Jeffersonquesado by default the Maven does not do the Property Replacement us Resources, therefore the filtering. Behold: Resource filter. The maven-filtering uses this behavior. Another thing: why it is considered preferable to use skipTests? In fact the maven.test.skip even avoids the compilation of tests, being considered by the surefire, Compile and failsafe - and anyone else who follows the pattern then it is up to "preferable" to save a little resource ;)

  • I just couldn’t get him to not create the dev file in the test environment and vice versa, although it will be empty, but for what I needed it served and I will still implement the email appender in the same way, since the client wants different groups of email to receive the logs according to the environment.

  • This should resolve createOnDemand Boolean The appender creates the file on-demand. The appender only creates the file when a log Event passes all Filters and is routed to this appender. Defaults to false.

  • @Otávio you do not need to have two appenders no, I saw that you just change the name of the log file. How about using the spring.profile everywhere you are using dev or test?

  • @Brunocésar ah yes, you’re right, thanks for the tip, just in case the email I need to change the recipients and host of smtp too, then I think I’ll need to use more. Thanks!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.