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

3

7

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>
Terrier answered 5/3, 2018 at 2:24 Comment(3)
Your going to get an answer with #35560324 tbhMiletus
I know that Spring Profiles, different Log4j2 configs solves my question, but it's not elegant and not recommended by spring-boot. So here am I to ask for help.Terrier
In logging.apache.org/log4j/2.x/log4j-spring-boot Log4J2 says it is working with the mentioned tag. I guess this maybe not supported out of the box by Spring Boot.Harbinger
H
5

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.

Harbinger answered 24/2, 2022 at 7:8 Comment(2)
To be historically accurate, the SpringProfile arbiter appeared in version 2.15.0 (see LOG4J2-3226) and was not available at the time this question was asked.Lubeck
Okay, I was missing that bit of information here. Thanks for adding that @Pior.Harbinger
H
1

You have to add

<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-spring-boot</artifactId>
</dependency>
Herriott answered 16/8, 2023 at 9:17 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Jaunitajaunt
P
0

There's no relationship (or at least not that I know) between Apache Log4J2 and the Spring Framework.

The SpringProfile tag you've used doesn't exist in the Log4J2's XML schema either.

You would have to play with different Spring Profile configurations and different Log4J2 configuration for each. On the other hand, Logback and Log4J2 are (almost completely) different libraries for the same purpose of logging.

Poynter answered 5/3, 2018 at 2:38 Comment(6)
But the spring-boot reference says that it could control the log initialization. Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xml or define a logging.config property.Terrier
I've tried exactly that with Logback...and indeed it works, but the same doesn't apply for Log4J2.Poynter
If you can use Logback with async appenders, you can get some pretty decent performance — comparable with Log4J2. In that case, you would need LMAX Disruptor additionally in your classpath.Poynter
Aha, thanks for more information. Next time I'm going to try to use logback instead of log4j2. But should I open an issue to spring-boot or log4j2, I wonder?Terrier
Today this is possible: https://mcmap.net/q/1520141/-why-i-can-39-t-use-springprofile-tag-in-log4j2-spring-xml-to-config-log4j2-with-different-spring-profilesHarbinger
The SpringProfile arbiter is discussed in the Log4J documentation: logging.apache.org/log4j/2.x/manual/configuration.html#arbitersHilltop

© 2022 - 2024 — McMap. All rights reserved.