SAP/cf-java-logging-support

log4j2 doesn't work in spring

ivanmir opened this issue · 4 comments

Even though we disable the standard logging in Spring and add the log4j2 implementation, the SAP Java Buildpack already loads the logback in its BOM. So, whenever you try to implement log4j2 configuration/setup it only works locally. In CF you will get errors about conflicting implementation related to logback and log4j2 being both in the path.

Did you disable logback in your Spring Boot configuration? Usually you would need to exclude the corresponding spring-boot-starter-logging and add spring-boot-startter-log4j2:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</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>

There is a nice description in the following blog post:
https://www.springboottutorial.com/logging-with-spring-boot-logback-slf4j-and-log4j

Yes I did. But there is a jar in the sap_java_buildpack for logback that gets loaded before log4j2.

This is really an issue with the buildpack, not this library. However, even in the presence of both jars, you can configure Spring to use the LoggingSystem of your choice:

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration

I have not tried this out, but I guess you want to have this combination:

org.springframework.boot.logging.LoggingSystem=org.springframework.boot.logging.log4j2.Log42LoggingSystem

See the source code of LoggingSystem for details of the implementation.

Hi Karsten,

I agree with you. This issue is more in terms of the buildpack.
I've tried the proposed solution, but it didn't work.
It gives the the following error message:

java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/home/vcap/app/WEB-INF/lib/log4j-slf4j-impl-2.11.2.jar)

Thanks any way for the help.

Best regards,
Ivan