引言: 一个问题的分析与解决过程是表与里的过程,是一个大胆猜测与小心求证的过程,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>

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>

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 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]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:91 - no applicable action for [RollingRandomAccessFile], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@17:106 - no applicable action for [PatternLayout], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][PatternLayout]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@18:23 - no applicable action for [Policies], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][Policies]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@19:45 - no applicable action for [TimeBasedTriggeringPolicy], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][Policies][TimeBasedTriggeringPolicy]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@21:48 - no applicable action for [DefaultRolloverStrategy], current ElementPath is [[Configuration][Appenders][RollingRandomAccessFile][DefaultRolloverStrategy]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:14 - no applicable action for [Loggers], current ElementPath is [[Configuration][Loggers]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@27:59 - no applicable action for [Logger], current ElementPath is [[Configuration][Loggers][Logger]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@30:69 - no applicable action for [Logger], current ElementPath is [[Configuration][Loggers][Logger]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@31:42 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Logger][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@32:46 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Logger][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@36:29 - no applicable action for [Root], current ElementPath is [[Configuration][Loggers][Root]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@37:41 - no applicable action for [AppenderRef], current ElementPath is [[Configuration][Loggers][Root][AppenderRef]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@38: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>

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排除操作,却并未完全进行替换掉,这个是在包的依赖中遗漏掉的。

Spring Boot与Log4j2集成之java.lang.IllegalStateException: Logback configuration error detected:相关推荐

  1. springboot集成logback,报错java.lang.IllegalStateException: Logback configuration error detected:

    最近需要将一个springboot项目部署到一台新服务器上.而且新服务器是完全copy了旧服务器的环境与项目.但是在新服务器上启动该springboot项目,就一直报错 2019-03-18 18:5 ...

  2. springboot配置log4j2报错:java.lang.IllegalStateException: Logback configuration error detected:

    引入log4j2后启动报错. Exception in thread "main" java.lang.IllegalStateException: java.lang.Illeg ...

  3. Spring Boot整合Nacos时遇到 java.lang.IllegalStateException: Context has been already given a name 的解决办法

    问题概述 在进行项目开发过程中,通过Spring Boot整合Alibaba Nacos的服务注册与发现和配置中心时,遇到个梗,启动时打印出一串错误日志信息 " Failed to rena ...

  4. spring boot 启动 nested exception is java.lang.IllegalStateException

    2019独角兽企业重金招聘Python工程师标准>>> 在启动spring boot 项目时报错,之前都是OK的,只是我引用了内置的tomcat包就报错了 <dependenc ...

  5. idea使用spring框架Exception in thread main java.lang.IllegalStateException错误

    自己在用idea自动导包使用spring框架的时候 出现如下错误异常: Exception in thread "main" java.lang.IllegalStateExcep ...

  6. Spring Cloud RestTemplate报错:java.lang.IllegalStateException: No instances available for xxx

    java.lang.IllegalStateException: No instances available for XXXX 一.问题复现 最近搭建springCloud项目,搭建Eureka,多 ...

  7. 关于IDEA启动Spring Boot项目出现Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext at j

    Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext at java.lang.Class.getDeclar ...

  8. spring boot Exception in Thread “main” java.lang.classNoFoundException

    在客户测试环境部署,通过打包成jar,使用命令 nohup java -jar /usr/local/tomcat/shirencai/ct-peixun-provider.jar –spring.p ...

  9. 【问题已解决】Caused by: java.lang.IllegalStateException

    错误 Caused by: java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not ...

最新文章

  1. HashTable, HashMap, LinkedHashMap, ConcurrentHashMap
  2. SQL Server查询性能优化——堆表、碎片与索引(一)
  3. IT人不要一辈子做技术
  4. 不当免费技术支持的10种方法
  5. NDK开发之日志打印
  6. (38)编写 ShellCode
  7. 共享计算机后无法访问磁盘,win10电脑共享硬盘无法访问如何解决
  8. 程序员求职面试丨面试必备之终极指导篇,掌握这些,面试不再困难!
  9. VC++开发演算稿式计算语言-可编程绘图的计算器
  10. ZooKeeper学习第四期---构建ZooKeeper应用
  11. 走进AngularJs(一)angular基本概念的认识与实战
  12. 管理感悟:计算缺陷的权重
  13. 渗透测试专业术语——防守篇
  14. PR连接蓝牙后无声音
  15. 企业微信:上传图片获取永久url --Java
  16. 机器人周志_关于机器人的日记
  17. 盛世长缨rt8188gu安装网卡驱动(Ubuntu)
  18. html css alpha,CSS滤镜之alpha属性-网页设计,HTML/CSS
  19. 回车、换行、空格的ASCII码值
  20. ROS——Teb算法的优化

热门文章

  1. [开心互动]网友视频--穷富大比拼,绝对今年最具创意的!!看谁更厉害
  2. 使用微软官方工具制作启动盘
  3. 影响UWB定位技术精度的8个原因
  4. 经济学原理知识点总结
  5. 小m序列的verilog实现
  6. 港科夜闻|香港科大叶玉如教授:打造香港科大2.0,为大湾区引进更多优质教育资源...
  7. 苹果手机如何多开双开 iPhone分身教程
  8. Microsoft Office 2016安装教程
  9. 初探BlockChain——哈希和电子签名
  10. 计算机软著评职称,软著可以评职称吗