logback的使用

1 logback概述

SLF4J的日志实现组件关系图如下所示。

logback的使用_第1张图片

SLF4J,即Java中的简单日志门面(Simple Logging Facade for Java),它为各种日志框架提供简单的抽象接口。

SLF4J最常用的日志实现框架是:log4j、logback。一般有slf4j+log4j、slf4j+log4j2、slf4j+logback三种日志组合。下面将介绍logback的简单使用和使用中遇到的常见问题。

2 引入依赖

        
        
            ch.qos.logback
            logback-classic
            1.2.3
        

3 新增配置

在resources文件夹下面创建logback.xml配置文件。举例如下。




    
        
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{1000}-%M:%L - %msg%n
            
        
    

    
        /app/logs/demoForStudy/logstash.log
        
            
            /app/logs/demoForStudy/logstash.%d{yyyy-MM-dd}-%i.log
            
            10MB
            2
            100MB
            
            true
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{1000}-%M:%L - %msg%n
        
    





    
          
           
    

参数说明如下。

  • 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.Log4jLoggerFactory
	at 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”直接排除此依赖。

操作截图如下所示。

logback的使用_第2张图片logback的使用_第3张图片logback的使用_第4张图片logback的使用_第5张图片

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配置

你可能感兴趣的:(日志框架,logback,LoggerFactory,slf4j)