Could not initialize Logback logging from classpath:logback-spring.xml 错误解决
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 file
、1 字节的 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.logfile> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error/error-%d{yyyy-MM-dd}.log fileNamePattern> <MaxHistory>30MaxHistory> // 以下忽略
并且跑到编译后的文件target
下看到如下情况:
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/error/error.logfile> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error/error-%d{yyyy-MM-dd}.log fileNamePattern> <MaxHistory>30MaxHistory> // 以下忽略
那问题应该就知道了,只是编译的过程中字符集编码有问题。
3. 问题解决
首先看了下自己的maven
编译的插件是哪个:
从idea里看到,生效的编译插件是maven-compiler-plugin
插件,然后我就到pom.xml
里找到它,发现它确实没有设置字符集编码,添加了
设置。
<plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.3version> <configuration> <source>1.8source> <target>1.8target> <encoding>UTF-8encoding> 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 file
和 X字节的 UTF-8 序列的字节 1 无效
的类似错误,基本都是编译插件未设置字符集编码,只要加上就可以。
来源地址:https://blog.csdn.net/wohaqiyi/article/details/128911614
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341