3

I am trying to let the embedded Tomcat in Spring Boot log to my Log4J2 configuration, but it doesn't work.

According to this answer that copes with an external Tomcat: https://stackoverflow.com/a/28639068/1845463 there seems to be the need to move some jars to $Catalina_home/libs etc. I think this is not possible with Spring Boot, isn't it?

Has someone managed to get log4j2 running and be able to configure appenders for catalina log?

Thanks in advance

Community
  • 1
  • 1
Stefano L
  • 1,486
  • 2
  • 15
  • 36

2 Answers2

1

EDITED: The simplest way to do is to add spring-boot-starter-log4j2.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

N.B. Make sure that the other components don't need different version of log4j. This may cause run-time errors. e.g. elasticsearch java API requires 2.6+ and spring-boot-starter-log4j2:1.3.8 provides log4j:2.4.1, if we're building an app that connects elasticsearch and uses spring boot too. Then we will end up getting NoSuchMethodError or similar errors. For resolution of those errors we should add log4j2:2.6+ in our pom.

  • What a convoluted way... Just add `spring-boot-starter-log4j2` instead of what you do. – M. Deinum May 05 '17 at 13:30
  • Tried but it does not work. It caused me a lot of NoSuchMethodErrors for org.apache.logging.log4j.Logger. –  May 05 '17 at 13:37
  • @M.Deinum No such method errors are resolved by adding log4j2-api and log4j2-core. I wonder if there is more appropriate way! –  May 05 '17 at 14:49
  • 1
    Adding core and api basically defeats the purpose of using the starter. The whole point of the starter is is that you don't need to add those dependency. If you get those no-such-method errors there is something else going on and you are probably messing things up with other dependencies. – M. Deinum May 06 '17 at 12:12
  • There are other dependencies in the starter as well, such as the code to bridge JUL into slf4j; the advantage of the starter is that it includes the pieces you didn't know you needed. – Donal Fellows Feb 07 '22 at 14:36
0

Which Spring Boot version are you using? I believe 1.4.x.RELEASE uplifted it to log4j2. As @M.Deinum mentioned including:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

brings in:

log4j-core, log4j-api and a few more. See https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2/1.4.6.RELEASE

It might be advisable to exclude logging starter using:

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
ootero
  • 3,235
  • 2
  • 16
  • 22