Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why I can't use springProfile tag in log4j2-spring.xml to config log4j2 with different spring profiles?

Well, I have 3 spring profiles: dev, prod, test, and I want to use different log4j2 configuration in different profiles. I checked the the spring-boot reference and followed the way it said. But when I run spring application, I only get the log below:

2018-03-05 09:52:32,194 main ERROR Error processing element SpringProfile ([Configuration: null]): CLASS_NOT_FOUND
2018-03-05 09:52:32,194 main ERROR Error processing element SpringProfile ([Configuration: null]): CLASS_NOT_FOUND

I googled and stackoverflowed the error log, and can't still find an answer why springProfile tag didn't work.

And here is my log4j2-spring.xml:

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

<Configuration>
    <SpringProfile name="prod">
        <Appenders>
            <RollingFile name="RollingFile"
                         fileName="/home/prod/service.log"
                         filePattern="/home/prod/service.log.%d{yyyyMMddHH}"
                         append="true">
                <PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                </Policies>
            </RollingFile>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="RollingFile"/>
            </Root>
        </Loggers>
    </SpringProfile>

    <SpringProfile name="!prod">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
            </Console>

            <File name="File" fileName="./logs/service.log" append="false">
                <PatternLayout pattern="[%level][%d{yyyy-MM-dd'T'HH:mm:ss.SSSXX}][%l] %msg%n" />
            </File>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="File"/>
                <!--When WIP, you could uncomment the next line to show log to console.-->
                <!--<AppenderRef ref="Console"/>-->
            </Root>
        </Loggers>
    </SpringProfile>
</Configuration>
like image 891
TaoSama Avatar asked Mar 05 '18 02:03

TaoSama


1 Answers

You are missing the dependency for Log4J2's spring boot support in your project. You need to add org.apache.logging.log4j:log4j-spring-boot. Adding this as dependency enables the usage of the SpringProfile tag. (This feature is available since version 2.15.0)

One might consider this a bug of the Spring Boot Log4J2 integration.

like image 150
Markus Avatar answered Oct 10 '22 05:10

Markus