1 logback概述
SLF4J與其它日志組件調用關系圖如下所示。
SLF4J,即Java中的簡單日志門面(Simple Logging Facade for Java),不是具體的日志解決方案,它只服務于各種各樣的日志系統。
SLF4J最常用的日志實現框架是:log4j、logback。一般有slf4j+log4j、slf4j+log4j2、slf4j+logback三種日志組合。下面將介紹logback的簡單使用和使用中遇到的常用問題的排查。
2 引入依賴
<!--logback--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency>
3 新增配置
在resources文件夾下面創建logback.xml配置文件。舉例如下。
<?xml version="1.0" encoding="UTF-8"?>
<configuration><appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{1000}-%M:%L - %msg%n</Pattern></layout></encoder></appender><appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>/app/logs/demoForStudy/logstash.log</file><rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"><!-- rollover daily --><FileNamePattern>/app/logs/demoForStudy/logstash.%d{yyyy-MM-dd}-%i.log</FileNamePattern><!-- each file should be at most 10MB, keep 2 days worth of history, but at most 100MB --><maxFileSize>10MB</maxFileSize><maxHistory>2</maxHistory><totalSizeCap>100MB</totalSizeCap><!-- archive removal will be executed on appender start up --><cleanHistoryOnStart>true</cleanHistoryOnStart></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{1000}-%M:%L - %msg%n</pattern></layout></appender><root level="INFO"><appender-ref ref="stdout" /> <!-- 輸出到控制臺 --><appender-ref ref="logstash" /> <!-- 輸出到logstash --></root></configuration>
4 問題排查
可能出現的問題如下。
4.1?LoggerFactory is not a Logback LoggerContext but Logback is on the classpath
4.1.1 報錯
啟動報錯如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/softwareInstall/maven/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/softwareInstall/maven/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.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 [org.slf4j.impl.Log4jLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from file:/softwareInstall/maven/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactoryat org.springframework.util.Assert.instanceCheckFailed(Assert.java:702)at org.springframework.util.Assert.isInstanceOf(Assert.java:621)at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:290)at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:117)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:232)at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:213)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:127)at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:76)at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:53)at java.util.ArrayList.forEach(ArrayList.java:1257)at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:53)at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)at com.hn.liao.study.DemoForStudyApplication.main(DemoForStudyApplication.java:37)
4.1.2 問題原因
從報錯可知,因為在項目中存在SLF4J的兩種日志橋接綁定或日志實現組件:slf4j-log4j12和logback-classic,即日志實現組件的包沖突導致項目啟動報錯。
4.1.3 排除slf4j-log4j12依賴
- 首先右鍵pom.xml文件,通過選項“Maven->show Depedencies”顯示項目中包的依賴關系;
- 然后在依賴關系文件中直接搜索“slf4j-log4j12”找到該包。
- 最后選中該包右鍵,通過選項“Exclude”直接排除此依賴。
操作截圖如下所示。
5 參考文獻
(1)Chapter 4: Appenders
(2)logback詳解
(3)IDEA 報錯 LoggerFactory is not a Logback LoggerContext but Logback is on the classpath