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><!-- <logger name="com.test" level="warn" additivity="false" >-->
<!-- <appender-ref ref="stdout" />-->
<!-- </logger>--><root level="INFO"><appender-ref ref="stdout" /> <!-- 输出到控制台 --><appender-ref ref="logstash" /> <!-- 输出到logstash --></root></configuration>
参数说明如下。
- appender(输出源):主要控制日志输出到哪里,比如:文件、数据库、控制台打印等。
- logger(记录器):用来设置某一个包或者具体某一个类的日志打印级别、以及指定 appender。
- root:也是一个 logger,是一个特殊的父 logger。所有的子 logger 最终都会将输出流交给 root,除非在子 logger 中配置了additivity=“false”。
- rollingPolicy:指定滚动策略,按照一定周期或文件大小切割存放日志文件,以及指定日志文件的最大大小。
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
(4)springboot 日志配置(logback)_springboot logback配置