1. 问题发生

  springboot项目以前运行时没有问题,但是重装系统换了个IDEA运行项目忽然报错如下:

Logging system failed to initialize using configuration from 'classpath:logback-spring.xml'java.lang.IllegalStateException: Could not initialize Logback logging from classpath:logback-spring.xmlat org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:168)at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132)at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:315)at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82)at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)at org.springframework.boot.SpringApplicationRunListeners$$Lambda$36/1293680734.accept(Unknown Source)at java.util.ArrayList.forEach(ArrayList.java:1249)at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:362)at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)at com.agri.WebStartApplication.main(WebStartApplication.java:20)Caused by: ch.qos.logback.core.joran.spi.JoranException: I/O error occurred while parsing xml fileat ch.qos.logback.core.joran.event.SaxEventRecorder.handleError(SaxEventRecorder.java:74)at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:62)at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:151)at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:188)at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165)... 23 moreCaused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:691)at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:557)at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1743)at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1256)at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanComment(XMLScanner.java:778)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanComment(XMLDocumentFragmentScannerImpl.java:1038)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2988)at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:59)... 28 more

2. 排查过程

  本来我只注意到Could not initialize Logback logging from classpath:logback-spring.xml这个提示,我以为是别人动到了代码,看了下提交记录也没有,经过网上排查,有说是logging.config=classpath:logback-spring.xml配置需要写,但是我这个是一直有的。

  经过仔细的排查上边的日志,后来发现报错下边还有一句I/O error occurred while parsing xml file1 字节的 UTF-8 序列的字节 1 无效。 并且对应的错误类都是XML开头,这问题看起来有点明显,应该是解析logback-spring.xml转换字符集编码问题。
 查看了下logback-spring.xml里,果然有中文注释:

<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/error/error.log</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/error/error-%d{yyyy-MM-dd}.log</fileNamePattern><MaxHistory>30</MaxHistory>// 以下忽略

   并且跑到编译后的文件target下看到如下情况:

 <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_PATH}/error/error.log</file><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/error/error-%d{yyyy-MM-dd}.log</fileNamePattern><MaxHistory>30</MaxHistory>// 以下忽略

   那问题应该就知道了,只是编译的过程中字符集编码有问题。

3. 问题解决

   首先看了下自己的maven编译的插件是哪个:

  从idea里看到,生效的编译插件是maven-compiler-plugin插件,然后我就到pom.xml里找到它,发现它确实没有设置字符集编码,添加了UTF-8设置。

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.3</version><configuration><source>1.8</source><target>1.8</target> <encoding>UTF-8</encoding></configuration></plugin>

   最后再重新编译,发现target下的xml文件正常了。

4. 扩展

   当然,因为日志文件在项目启动最早需要用到,所以报错Could not initialize Logback logging from classpath:logback-spring.xml 只是开始,假如你只是单纯的把logback-spring.xml文件里的中文删除掉,并不能完整解决此问题。
  因为我们项目可能还有些xml带中文注释,比如mapper.xml,这种情况下也可能报错什么X字节的 UTF-8 序列的字节 1 无效
 因此,项目中假如遇到I/O error occurred while parsing xml fileX字节的 UTF-8 序列的字节 1 无效的类似错误,基本都是编译插件未设置字符集编码,只要加上就可以。