天天看點

Spring Boot與Log4j2內建之java.lang.IllegalStateException: Logback configuration error detected:

引言: 一個問題的分析與解決過程是表與裡的過程,是一個大膽猜測與小心求證的過程,spring boot與log4j2的內建過程中,我将描述一下分析這個問題的思路和過程。 我一直強調一點: 重要的不是解決問題的結論,而是解決問題的思路和方法,即使在解決完問題之後,依然需要回過頭複盤,在問題分析過程中的走過的彎路。

1 項目情況介紹

Spring Boot 1.5.7 , JDK 1.8, Log4j2.7 

在這個項目中,希望內建Log4j2作為預設的日志系統輸出器。 

Log4j2.xml的定義如下:

"><Configuration status="off">

    <Properties>

        <Property name="LOG_PATH">.</Property>

    </Properties>

    <Appenders>

        <!-- Console -->

        <Console name="Console" target="SYSTEM_OUT">

            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36}.%M@%L - %msg%n"/>

            <!--<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %C{5} (%F:%L) - %msg%n"/>-->

        </Console>

        <!-- RollingFile -->

        <RollingRandomAccessFile name="RollingFile"

                                 fileName="./aifasion.log"

                                 filePattern="${LOG_PATH}/aifasion.log.%d{yyyy-MM-dd-HH}">

            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36}.%M@%L - %msg%n"/>

            <Policies>

                <TimeBasedTriggeringPolicy/>

            </Policies>

            <DefaultRolloverStrategy max="30"/>

        </RollingRandomAccessFile>

   </Appenders>

    <Loggers>

        <!-- Spring Loggers -->

        <Logger  name="org.springframework" level="info"/>

        <!-- App Loggers -->

        <Logger name="org.jd.test" level="debug" additivity="false">

            <AppenderRef  ref="Console"/>

            <AppenderRef  ref="RollingFile"/>

        </Logger>

        <!-- Root Logger -->

        <Root  level="info">

            <AppenderRef ref="Console"/>

            <AppenderRef ref="RollingFile"/>

        </Root >

    </Loggers>

</Configuration>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

application.properties的檔案定義如下:

server.port=8080 

logging.config=classpath:log4j2.xml 

debug=true

上述為核心的配置檔案資訊。 

在pom.xml中,針對spring-boot-web中對于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>

            </exclusions>

        </dependency>

1

2

3

4

5

6

7

8

9

10

11

2 問題描述

基于Spring Boot的向導建立基礎項目,內建Log4j2,在項目啟動過程中,報出如下的錯誤資訊:

SLF4J: Class path contains multiple SLF4J bindings. 

SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class] 

SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.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] 

The Class-Path manifest attribute in D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derby-10.13.1.1.jar referenced one or more files that do not exist: D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_cs.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_de_DE.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_es.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_fr.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_hu.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_it.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_ja_JP.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_ko_KR.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_pl.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_pt_BR.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_ru.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_zh_CN.jar,D:\DevSpace\M2Space\org\apache\derby\derby\10.13.1.1\derbyLocale_zh_TW.jar 

10:00:46.441 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 

10:00:46.444 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/] 

10:00:46.444 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/D:/workspace/testdemo/target/classes/] 

Logging system failed to initialize using configuration from ‘classpath:log4j2.xml’ 

java.lang.IllegalStateException: Logback configuration error detected: 

ERROR in [email protected]:17 - no applicable action for [Properties], current ElementPath is [[Configuration][Properties]] 

ERROR in [email protected]:35 - no applicable action for [Property], current ElementPath is [[Configuration][Properties][Property]] 

ERROR in [email protected]:16 - no applicable action for [Appenders], current ElementPath is [[Configuration][Appenders]] 

ERROR in [email protected]:53 - no applicable action for [Console], current ElementPath is [[Configuration][Appenders][Console]] 

ERROR in [email protected]:92 - no applicable action for [PatternLayout], current ElementPath is [[Configuration][Appenders][Console][PatternLayout]] 

ERROR in [email protected]:91 - no applicable action for [RollingRandomAccessFile], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile]] 

ERROR in [email protected]:106 - no applicable action for [PatternLayout], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][PatternLayout]] 

ERROR in [email protected]:23 - no applicable action for [Policies], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][Policies]] 

ERROR in [email protected]:45 - no applicable action for [TimeBasedTriggeringPolicy], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][Policies][TimeBasedTriggeringPolicy]] 

ERROR in [email protected]:48 - no applicable action for [DefaultRolloverStrategy], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][DefaultRolloverStrategy]] 

ERROR in [email protected]:14 - no applicable action for [Loggers], current ElementPath is [[Configuration][Loggers]] 

ERROR in [email protected]:59 - no applicable action for [Logger], current ElementPath is [[Configuration][Loggers][Logger]] 

ERROR in [email protected]:69 - no applicable action for [Logger], current ElementPath is [[Configuration][Loggers][Logger]] 

ERROR in [email protected]:42 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Logger][AppenderRef]] 

ERROR in [email protected]:46 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Logger][AppenderRef]] 

ERROR in [email protected]:29 - no applicable action for [Root], current ElementPath is [[Configuration][Loggers][Root]] 

ERROR in [email protected]:41 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Root][AppenderRef]] 

ERROR in [email protected]:45 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Root][AppenderRef]] 

at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162) 

at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66) 

at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56) 

at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115) 

at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308) 

at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276) 

at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239) 

at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212) 

at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) 

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) 

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) 

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) 

at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) 

at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) 

at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) 

at org.springframework.boot.SpringApplication.run(SpringApplication.java:296) 

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) 

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) 

at org.jd.test.TestdemoApplication.main(TestdemoApplication.java:14) 

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 

at java.lang.reflect.Method.invoke(Unknown Source) 

at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

以上是在系統啟動過程中的錯誤資訊,關鍵錯誤資訊是:

Logging system failed to initialize using configuration from ‘classpath:log4j2.xml’ 

java.lang.IllegalStateException: Logback configuration error detected:

3 問題分析過程

3.1 配置檔案的格式或者定義錯誤

根據剛才看到的錯誤資訊,第一反應是log4j2.xml中定義的錯誤資訊或者格式資訊有誤。 

根據這個判斷,就直接在Log4j2的官方文檔上,重新查閱了一番,結合定義中的内容格式,感覺格式都是完全正确的,沒有什麼出入問題。 

于是,我就使用排除法,将Log4j2.xml中的内容按照步驟,一部分一部分的進行驗證排除,如果哪個部分中存在配置檔案的配置錯誤,則可以定位出來。 

在一番定位之後,發現隻要配置檔案中存在内容,就會報出類似錯誤資訊。 

結論是,應該不是配置檔案的配置錯誤資訊。

3.2 下載下傳樣例項目進行分析

于是,從網上直接下來了spring-boot中自帶的spring-boot-sample-actuator-log4j2項目,進行分析: 

發現其中pom.xml中的不同之處為: 

<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> 

而之前的exclusion是在spring-boot-web-starter中排除的,就是這各位置的不同造成的。 

換個角度來分析,可以得出在spring-boot-web-starter中exclude掉的logging并未完全清楚幹淨,還有其他的類庫或者包存在類似的以來。

4. 問題的解決

将spring-boot-starter-logging的包在spring-boot-starter中直接排除掉,從spring boot整體上進行替換,就可以解決這個問題:

    <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>

1

2

3

4

5

6

7

8

9

10

5 反思分析過程

在這個分析過程中,在看到日志的錯誤之時,第一反應就是配置檔案的配置錯誤,這個方向從一開始就是錯誤的。 

在Pom.xml中雖然進行了exclude操作,但是并未真正的徹底排除掉logging;在看到樣例程式之後,才感覺到時logging的遺漏引用存在導緻了問題。 

我又重新檢視一遍項目中報出的異常日志,發現了一個我疏忽的細節問題:

SLF4J: Class path contains multiple SLF4J bindings. 

SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class] 

SLF4J: Found binding in [jar:file:/D:/DevSpace/M2Space/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.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]

在這個提示資訊裡面,明确了告知logging有多個綁定,logback、log4j2兩種實作;目前使用的是logback的綁定,這個提示資訊竟然忽略了…….

其實這個提示資訊已經明确告知了問題出現的根源,隻是當時被無意或者有意的忽略了…..

6 總結

經過這個問題的分析過程,我們可以得知,異常日志或者錯誤日志的分析是第一現場,非常的重要,它給了我們大量而豐富的第一手資訊,為我們分析和解決問題提示了非常多的方向,需要進行慎重和小心的求證與驗證。 

之後是exclude logging的依賴,雖然進行exclude排除操作,卻并未完全進行替換掉,這個是在包的依賴中遺漏掉的。

--------------------- 

作者:bladestone 

來源:CSDN 

原文:https://blog.csdn.net/blueheart20/article/details/78111350 

版權聲明:本文為部落客原創文章,轉載請附上博文連結!

繼續閱讀