1. Overview
In this article, we will see the various possibilities to prevent the error “LoggerFactory is not a Logback LoggerContext but Logback is on the classpath“.
2. Spring Boot default behavior
The Spring Boot auto-configures the logging framework based on the dependencies available in the class path. If the Log back dependency is available in the class path, then Spring boot considers it as the first choice.
The spring-boot-starter-logging
is the starter for the LogBack
logging. So if you include the spring-boot-starter-logging
starter dependency in your project, then Spring boot auto-configures the Log Back as the default logging framework.
The spring-boot-starter-web
starter dependency also contains the
starter. So declaring the spring-boot-starter-logging
web
starter in your project transitively pulls the LogBack
starter.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
If your project has either logging
or web
starter dependencies in the class path, then the Spring only auto-configures the LogBack
library as the first choice.
If your project has Logback and also log4j2 dependencies in the class path, then the Spring Boot configures the Log Back library instead of log4j2 dependency.
2.1. Fix for “LoggerFactory is not a Logback LoggerContext but Logback is on the classpath”
The mentioned error appears when you configure the log 4j library in your Spring Boot project.
Assume you have the web starter dependency and also has the log 4j2 library as below.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
The Spring Boot will still configure the LogBack library because of the web starter. If you want to use log4j, then you must exclude this default logging by using the following ways.
2.2. Exclude default LogBack on the maven
You can prevent the default Log Back configuration in your maven project by excluding the library spring-boot-starter-logging
from your class path as below:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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>
2.3. Exclude the default LogBack on the Gradle
In Gradle, we have different ways to exclude the default Log Back. You can exclude the spring-boot-starter-logging
module from all jar files specified in the dependency.
configurations { all*.exclude module : 'spring-boot-starter-logging' all*.exclude module : 'logback-classic' }
Alternatively, you can use the module replacement feature of Gradle. After declaring the log4j 2 dependency, instruct Gradle to replace all occurrences of the default logging (spring-boot-starter-logging) by Log4j 2 starter as below:
dependencies { implementation "org.springframework.boot:spring-boot-starter-log4j2" modules { module("org.springframework.boot:spring-boot-starter-logging") { replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback") } } }
3. Conclusion
To sum up, we have learned the ways to prevent the error LoggerFactory is not a Logback LoggerContext but Logback is on the classpath.