您好,登錄后才能下訂單哦!
這篇文章主要講解了“Springboot如何使用Logback實現日志配置”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Springboot如何使用Logback實現日志配置”吧!
默認情況下,SpringBoot內部使用logback作為系統日志實現的框架,將日志輸出到控制臺,不會寫到日志文件。如果在application.properties或application.yml配置,這樣只能配置簡單的場景,保存路徑、日志格式等。復雜的場景(區分 info 和 error 的日志、每天產生一個日志文件等)滿足不了,只能自定義配置文件logback-spring.xml或者logback.xml。
首先我們先了解下logback。
logback 主要分為三個模塊:
logback-core:是其他兩個模塊的基礎模塊
logback-classic:是對 core 模塊的擴展,相當于 log4j 的改良版。classic 模塊實現了 Slf4j 的 API 因此可以便于和其他日志框架直接切換
logback-access:與Servlet容器集成,以提供http訪問日志功能。
1.application .properties
# 日志打印信息配置
logging.config=classpath:logback-spring.xml
2.logback-spring.xml
<?mybatis version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <contextName>logback</contextName> <!--輸出到控制臺--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter>--> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 日志文件會滾動 --> <!-- 文件路徑 --> <file>./logs/gf_scanning.log</file><!-- 當前的日志文件存放路徑 --> <!-- 日志滾動策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 歷史日志文件的存放路徑和名稱 --> <fileNamePattern>./logs/gf_scanning.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <!-- 日志文件最大的保存歷史 數量--> <maxHistory>30</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> <!-- 日志文件中日志的格式 --> </encoder> </appender> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> </root> </configuration>
遇到個奇怪的問題,按上述配置完成logback的配置后,大多項目可以正常運行并保存日志到文件,但有一個項目啟動時報錯。java.lang.IllegalStateException: Could not initialize Logback logging from classpath:logback-spring.xml ... Caused by: ch.qos.logback.core.joran.spi.JoranException: Parser configuration error occurred
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.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:316)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:282)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:240)
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:85)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:339)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:297)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at cn.piesat.ScanningApplication.main(ScanningApplication.java:18)
Caused by: ch.qos.logback.core.joran.spi.JoranException: Parser configuration error occurred
at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:89)
at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:57)
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)
... 22 more
Caused by: org.xml.sax.SAXNotSupportedException: Feature: http://xml.org/sax/features/external-general-entities
at org.apache.crimson.parser.XMLReaderImpl.setFeature(XMLReaderImpl.java:211)
at org.apache.crimson.jaxp.SAXParserImpl.setFeatures(SAXParserImpl.java:143)
at org.apache.crimson.jaxp.SAXParserImpl.<init>(SAXParserImpl.java:126)
at org.apache.crimson.jaxp.SAXParserFactoryImpl.newSAXParserImpl(SAXParserFactoryImpl.java:113)
at org.apache.crimson.jaxp.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:141)
at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:82)
... 28 more
Disconnected from the target VM, address: '127.0.0.1:64222', transport: 'socket'Process finished with exit code 1
可能是由于jar包沖突,在pom文件中單獨引入logback-core的依賴后,問題解決。
<!-- logback 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.7</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.7</version> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </exclusion> </exclusions> </dependency>
感謝各位的閱讀,以上就是“Springboot如何使用Logback實現日志配置”的內容了,經過本文的學習后,相信大家對Springboot如何使用Logback實現日志配置這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。