0

I have a project whereby the latest build is now SpringBoot 2.6.0 (and older one is a WAR for Tomcat 8). I have a sub-project/component that connects to stuff that forces me to use Log4J1. Logback was working beautifully but I've had to disable it (due to Log4J2 eclipsing and breaking the code that requires Log4J1 and also a new vendor component requiring Log4J2 as well).

I've:

  1. disabled Logback by excluding spring-boot-starter-logging
  2. Added a dependency for spring-boot-starter-log4j

Via the Dependency hierarchy tab (in Eclipse) I can see the jcl and jul libraries and in my logs (Console and File appenders) it is clear that SpringBoot (jcl stuff) and any of my project jcl calls are logged, but any JUL are not.

I've read tons of different tickets on here, gleaned together the info and got to this point. So it's all good except I cannot seem to pipe the JUL calls to the Log4J1 setup.

I can see jul-to-slf4j brought in via spring-boot-starter-log4j.

I thought I'd write here as I'm sure it's something simple I've missed but I think I've been staring at it for too long this afternoon! :)

What have I missed?

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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-log4j</artifactId>
        <version>1.3.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.32</version>
    </dependency>   
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.32</version>
    </dependency>   
TilleyTech
  • 441
  • 5
  • 13
  • 1
    Just replace the log4j dependency with `log4j-over-slf4j` and all log4j logging will be redirected to SLF4j which in turn can do logback logging. This should already work out-of-the-box and you might just need to exclude log4j from your other dependency. Another note you are now mixing jars from different versions of Spring Boot, this will, at some point, come back to haunt you with nice and weird bugs. – M. Deinum Feb 08 '22 at 12:25
  • Thanks mate but I had to disable logback as it was pulling in Log4J2. Sadly I have to use Log4J1 due to an internal component and other system I have to connect to for realtime data. I posted how I got it working below actually as I managed to get it going! :) – TilleyTech Feb 08 '22 at 13:03
  • @Delirium - thanks for note on different versions. I will see how I can alter that. Noted about mixing! – TilleyTech Feb 08 '22 at 13:04
  • Also, I'll be asking when this other internal system will be moving on from Log4J1......!!!! :) – TilleyTech Feb 08 '22 at 13:06
  • 1
    logback isn't pulling in log4j2 it is only pulling in the log4j2 API not the core. This is needed so that logging from LOG4J2 can also be redirected to slf4j. So no it isn't pulling in log4j2. Write a Logback appender for that system (you can probably re-use much of the LOG4j one) and redirect all logging. Those workarounds you needed to do are already in place if you use the regular Spring BOot setup (for which log4j1 has been removed in newer versions). Finally you can still use SLF4J to redirect to log4j1 and you can probably reuse some of the old code from Spring Boot 1 to initialize it. – M. Deinum Feb 08 '22 at 13:10
  • Ah OK I hear you. For the vendor core of the project I'm working on I need to use Springboot 2.6.0. – TilleyTech Feb 08 '22 at 13:15

1 Answers1

0

I got the JUL bits piping via adding this:

SLF4JBridgeHandler.install();
java.util.logging.LogManager.getLogManager().getLogger("").setLevel( Level.INFO);

Which I found on question JUL to SLF4J Bridge

TilleyTech
  • 441
  • 5
  • 13