By Jitwxs Link: jitwxs.cn/e2390047.ht…
One, foreword
Slf4j is typically chosen as the logging facade for Java development, but the logging implementation is not the same. If the system is running with multiple logging implementations, a Warning similar to the following appears.
Second, the cause of the problem
We know that the default logging implementation used by SpringBoot is Logback, so we repeated the above error when we tried to introduce Log4j’s dependency into our project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Copy the code
The above error tells us that there are multiple SLF4J bingdings, one in the LogBack and log4J package, with two StaticloggerBinders.
We know that with Slf4j, we need the loggerFactory.getLogger () method to get the instance.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Copy the code
private final Logger logs = LoggerFactory.getLogger(xxx.class); We can use this as an entry point to see the implementation of the source code. As shown below, I’ve highlighted the core code to focus on.
(1) Call getILoggerFactory() to get LoggerFactory.
(2) For the first call, INITIALIZATION_STATE should be UNINITIALIZED, so enter the initialization logic and call method performInitialization().
(3) Call bind().
(4) If not isAndroid(), call
FindPossibleStaticLoggerBinderPathSet () method, therefore, to find the possible staticLoggerBinder, note here return type is SET, which may be more.
(5) in findPossibleStaticLoggerBinderPathSet () this method, First by this loaded the org/slf4j/impl/StaticLoggerBinder. The class of this class path, it may be more, so using a while to get all the path, and eventually return.
(6) The reportActualBinding() method validates the SET size. If it is greater than 1, it prints the Warning we saw initially.
Third, problem solving
The IDEA is to remove unwanted logging implementations from dependency packages. Diagrams provided by IDEA make it easy to view dependencies in a project.
Open the PROJECT’s POM file and right-click Diagrams -> Show Dependencies
Suppose we want to exclude logback dependencies and use log4j. Ctrl + F searches logback to find the tree structure that references the dependency.
Click the icon in the image below in the upper left corner of the window to see only the currently selected dependency relationship.
The result is as follows:
As shown in the figure above, logback is introduced by spring-boot-starter-logging, and at the top level by spring-boot-starter-web and spring-boot-starter-test.
We tried to exclude this dependency in spring-boot-starter-web, and it should work. If the logback dependency still exists after the research after exclusion, the exclusion operation is repeated.
<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>
Copy the code
Four,
Logging framework conflicts can be a headache for beginners in particular because of the logging interface and logging implementation involved.
We believe in interface oriented programming, so we should take advantage of Maven’s delivery mechanism, from large open source projects to small corporate public JARS. The specific logging implementation should not be passed away to avoid affecting the downstream side of the call.
<optional>true</optional>
Copy the code
Recent hot articles recommended:
1.1,000+ Java Interview Questions and Answers (2021)
2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!
3. Oh, my gosh! What new syntax is xx ≠ null in Java?
4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!
5. “Java Development Manual (Songshan version)” the latest release, quick download!
Feel good, don’t forget to click on + forward oh!