前言
都知道log4j2比系統默認的日志要好,所以我們在整合springboot框架的時候會引入log4j2,但是我們也必須要把框架本來的日志要去掉才行,就像是不是親生的一樣,一個道理。
不想聽廢話的直接轉到第四步。
第一步
第一步當然是上網搜索啦,於是你會搜到很多相關的博客,這個時候你可能會找幾個看一看,看一下評論和閱讀量,然后才決定要不然安裝這個來操作,我也是這樣的:

按照網上的要求,做了很多變更,但是最后還是沒有配置成功,因為springboot本身的依賴默認是logging,而且springboot默認的組件中很多都依賴了logging。
首先你要整合。經過你綜合比較,你會選擇一個你覺得靠得住的,來給你的項目進行操作。這個步驟也簡單,一般就三個步驟:
- pom文件導入依賴log4j2
- 新建log4j2.xml的配置文件(命名系統默認的是log4j2-spring.xml,不過這個隨意的,反正下一步就要配置了)
- 在springboot的配置文件application中進行配置,指定配置文件
一般情況下,教程就結束了。
第二步
這個時候你會發現你運行沒有成功,是什么錯誤呢?
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/org/apache/logging/log4j/log4j-slf4j-impl/2.10.0/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
13:55:11.713 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
13:55:11.721 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
13:55:11.722 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/D:/CodeSpace/photobuyapi/target/classes/]
Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:17 - no applicable action for [properties], current ElementPath is [[configuration][properties]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:35 - no applicable action for [property], current ElementPath is [[configuration][properties][property]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@7:16 - no applicable action for [appenders], current ElementPath is [[configuration][appenders]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@9:53 - no applicable action for [Console], current ElementPath is [[configuration][appenders][Console]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@10:92 - no applicable action for [PatternLayout], current ElementPath is [[configuration][appenders][Console][PatternLayout]]
可能就是這種錯誤,然后你就瘋狂的查看是不是自己哪個步驟不對,然后再到網上搜
可能就是這個答案:
https://blog.csdn.net/blueheart20/article/details/80363870
告訴你要排除依賴,因為springboot本身的依賴中就有logging的依賴,你要去除,
然后你也去掉了,你可能去掉的是下面這個:
<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> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency>
當然你也可能比我少,但是你這個時候是否想到了一個問題,springboot只有這一個依賴是有logging的?
當然不是,如果你導入的依賴比較多的話,那么你需要逐個檢查一遍你的依賴中是否包含logging,怎么檢查?
第三步
首先要看你的項目現有的依賴:

這個里面存放的是你現在項目導入的依賴,你需要查看是否有如下幾個:

有的話,說明你的依賴中還是沒有把本身(logback)的日志去掉,如何去掉?
怎么查看有哪些依賴引入了logging?
第四步
你的依賴中還是沒有把本身(logback)的日志去掉,如何去掉?
怎么查看有哪些依賴引入了logging?

查看依賴中是<artifactId>mybatis-spring-boot-starter</artifactId> 或者<artifactId>mybatis-spring-boot-starter + jdbc或者是其他的
你按住ctrl +鼠標左鍵 點進去:

會出現如下頁面:

在下面你會找到這個:

點進去你會發現是這個:

你需要做的就是把所有有這種的依賴去掉logging的引入:

<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> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency>
把紅色的加上。
記住是所有的依賴,比如
<artifactId>mybatis-spring-boot-starter</artifactId> 這個也有,你要逐個查看,直到項目依賴中沒有

如有錯誤歡迎指證。
