0

We have a spring boot application using log4j2.

We want to use different log levels for different profiles. After lot of struggles, I found out that this is not possible with log4j2 but it is possible with logback. I don't want to use logback though.

Reason this is not possible with log4j2 is - spring boot initializes log4j2 well before it loads application properties.

So, I have initialized log4j2 with all the default values in log4j2.xml and after the application has started up, I'm getting the logger context and changing the log levels as per the profile specific log levels programmatically.

Though this is working, just wanted to understand if there is any better way.

1 Answers1

1

According to this blog, separating log4j2 logging between profiles is possible. I also did it.

Why don't you create log4j2.xml based on profiles?. like below

spring:
  config:
    activate:
      on-profile: dev

logging:
  config: classpath:log4j2-dev.xml

---
spring:
  config:
    activate:
      on-profile: sandbox

logging:
  config: classpath:log4j2-sb.xml
HYUNJUN
  • 49
  • 6
  • I tried this. Doesn't really work. I checked LoggerContext after spring boot is completely initialized. It has configurationSource = NULL_SOURCE. I tried - LoggerContext.getContext().setConfigLocation(env.getProperty("logging.config")); configurationSource then got updated to the right path. – Santosh Kumar Nov 07 '21 at 09:53