Could not initialize Logback logging from classpath:logback-spring.xml 错误解决

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.xml
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:168)
	at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecifi***onfig(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.a***ept(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 ***.agri.WebStartApplication.main(WebStartApplication.java:20)
Caused by: ch.qos.logback.core.joran.spi.JoranException: I/O error o***urred while parsing xml file
	at 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.Generi***onfigurator.doConfigure(Generi***onfigurator.java:151)
	at ch.qos.logback.core.joran.Generi***onfigurator.doConfigure(Generi***onfigurator.java:110)
	at ch.qos.logback.core.joran.Generi***onfigurator.doConfigure(Generi***onfigurator.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 more
Caused by: ***.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。
	at ***.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:691)
	at ***.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:557)
	at ***.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1743)
	at ***.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1256)
	at ***.sun.org.apache.xerces.internal.impl.XMLScanner.scan***ment(XMLScanner.java:778)
	at ***.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scan***ment(XMLDocumentFragmentScannerImpl.java:1038)
	at ***.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2988)
	at ***.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
	at ***.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
	at ***.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at ***.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
	at ***.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
	at ***.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at ***.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
	at ***.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
	at ***.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 o***urred while parsing xml file1 字节的 UTF-8 序列的字节 1 无效。 并且对应的错误类都是XML开头,这问题看起来有点明显,应该是解析logback-spring.xml转换字符集编码问题。
  查看了下logback-spring.xml里,果然有中文注释:

<!-- ERROR -->  
    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <!-- 文件路径 ,注意LOG_PATH是默认值,
        	它的配置对应application.properties里的logging.path值-->  
        <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下看到如下情况:

 <!-- ERROR -->  
    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <!-- �ļ�·�� ��ע��LOG_PATH��Ĭ��ֵ��
        	�������ö�Ӧapplication.properties���logging.pathֵ-->  
        <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-***piler-plugin插件,然后我就到pom.xml里找到它,发现它确实没有设置字符集编码,添加了<encoding>UTF-8</encoding>设置。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-***piler-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 o***urred while parsing xml fileX字节的 UTF-8 序列的字节 1 无效的类似错误,基本都是编译插件未设置字符集编码,只要加上就可以。

转载请说明出处内容投诉
CSS教程_站长资源网 » Could not initialize Logback logging from classpath:logback-spring.xml 错误解决

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买