SpringBoot Log4J2 Profiles

In this article, I’ll keep used a simple approach to achieve the profiling for log4j2.

In order to used the log4j2 in springboot, we need to exclude the default logback from our dependencies, and add the log4j2 dependencies library.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

application.properties

I’m making used of properties as variables string to reference to the log4j2 configuration files.

spring.profiles.active=production
logging.config=classpath:log4j2-${spring.profiles.active}.xml

We cannot used springProfile in logback in log4j2.xml therefore we need to use different approach by creating 2 configuration, log4j2-local.xml and log4j2-production.xml which corresponding to the spring.profiles.active variable.

log4j2-local.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="DEBUG">

    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="org.hibernate.SQL" level="debug" additivity="false">
            <AppenderRef ref="STDOUT"/>
        </Logger>
        <Logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">
            <AppenderRef ref="STDOUT"/>
        </Logger>
        <Logger name="org.springframework.boot" level="info" additivity="false">
            <AppenderRef ref="STDOUT"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>



</Configuration>

log4j2-production.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>

    <Properties>
        <Property name="LOG_HOME" value="/Users/cheemingloong/Documents/workspace-hibernate-jpa/hibernate-many-to-one/logs/" />
        <Property name="LOG_NAME" value="production-log" />
    </Properties>

    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
        </Console>
        <File name="FILE" fileName="${LOG_HOME}/${LOG_NAME}">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>

    <Loggers>
        <Logger name="org.springframework.boot" level="info" additivity="false">
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="FILE"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="STDOUT"/>
            <AppenderRef ref="FILE"/>
        </Root>
    </Loggers>

</Configuration>
SpringBoot Log4J2 Profiles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.