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>
TaoSama
  • 83
  • 1
  • 8
  • Your going to get an answer with https://stackoverflow.com/questions/35559824/spring-profiles-different-log4j2-configs tbh – gsquaredxc Mar 05 '18 at 02:33
  • I know that [Spring Profiles, different Log4j2 configs](https://stackoverflow.com/questions/35559824/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. – TaoSama Mar 05 '18 at 02:37
  • In https://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. – Markus Feb 24 '22 at 06:47

3 Answers3

3

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.

Markus
  • 2,071
  • 4
  • 22
  • 44
  • To be historically accurate, the `SpringProfile` arbiter appeared in version 2.15.0 (see [LOG4J2-3226](https://issues.apache.org/jira/browse/LOG4J2-3226)) and was not available at the time this question was asked. – Piotr P. Karwasz Feb 24 '22 at 07:42
  • Okay, I was missing that bit of information here. Thanks for adding that @Pior. – Markus Feb 24 '22 at 08:20
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.

x80486
  • 6,627
  • 5
  • 52
  • 111
  • 2
    But the [spring-boot reference](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-custom-log-configuration) 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.` – TaoSama Mar 05 '18 at 02:41
  • I've tried exactly that with Logback...and indeed it works, but the same doesn't apply for Log4J2. – x80486 Mar 05 '18 at 02:43
  • 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. – x80486 Mar 05 '18 at 02:47
  • 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? – TaoSama Mar 05 '18 at 02:57
  • 1
    Today this is possible: https://stackoverflow.com/a/71248288/1251613 – Markus Mar 14 '22 at 14:38
0

You have to add

<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-spring-boot</artifactId>
</dependency>
coder47
  • 11
  • 1
  • 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](/help/how-to-answer). – Community Aug 19 '23 at 06:37