Home > Net >  How to disable Log4j configuration from external Maven dependency?
How to disable Log4j configuration from external Maven dependency?

Time:12-05

I have Spring Boot application with Log4j2 XML configuration file placed in resources/log4j2.xml. One external library I use is installed via Maven dependency and have own logging configuration in logback.xml.It seems that this file overwrites my Log4J2 configuration and logging is now controlled by this config file.

I'm getting logger instance (org.apache.logging.log4j.Logger) this way:

private static final Logger LOGGER = LogManager.getLogger(Foo.class);

Q: How can I disable Log4J configuration from external library?

Edit 1: Added Maven dependencies related to Log4j2

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

CodePudding user response:

In order to disable the external library's Log4J2 configuration, you can specify the -Dlog4j.configurationFile system property when starting your Spring Boot application, pointing it to the location of your own Log4J2 configuration file. For example, if your configuration file is in the resources directory of your project, you can use the following command to start your application:

java -Dlog4j.configurationFile=classpath:resources/log4j2.xml -jar your-application.jar

This will tell Log4J2 to use your configuration file instead of any other configuration files it may find on the classpath.

CodePudding user response:

Shortly:

The problem in your case is Spring try to use slf4j first (which is part of logback-classic) and Spring get it, due to logback exists in your classpath at runtime.

Detailed:

Spring-JCL under hood try to init logging system by searching classes in classloader in next order:

  1. try to load Log4j (if slf4j available load slf4j bridge)
  2. try to load Log4j
  3. try to load slf4j
  4. load java util logging

Full logic is available here: github spring source code

After that Spring boot start to config logger (using LoggingApplicationListener). In this phase Spring Boot configure logger using config file and available logger from classpath. In your case one of the dependencies uses logback-classic as a dependency and as a result it is available for springboot in runtime, that is why it uses logback-classic.

There are a few possible ways how to solve your problem:

  1. Migrate to logback-classic and use it in your project (it is preferable)
  2. If it is hard to change log4j2 to logback-classic, you can support both of them (for your code config from log4j2.xml will be used and logback.xml will be used for spring). It is necessary to add logging.config configuration to application.properties with path to your custom logback.xml - doc
  3. If you want to use log4j2 as a logger for springboot itself, it is necessary to exclude logback-classic from your dependency(it can lead to problems with dependency, which use it)
  • Related