logback配置详解

一,pom.xml导入依赖


      ch.qos.logback
      logback-classic
      1.2.3

二、添加配置文件: logback.xml

配置文件结点如下:
logback配置详解_第1张图片
2.1:appender标签
appender组件主要负责打印日志,设置日志输出位置,日志文件的生成策略等
1)ConsoleAppender:把日志打印到控制台
2)FileAppender:把日志输出到文件
3)RollingFileAppender:滚动记录地把日志输出到文件,支持日志文件按时间或文件大小拆分

下面以打印控制台日志和打印debug日志的代码片段进行说明:


    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    	
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>




    <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.filePath}/debug.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gzfileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
        rollingPolicy>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUGlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

2.2:logger标签
1)设置logger:用来设置某个包或某个指定类的日志打印级别,logger仅有一个name属性,一个可选的level和一个可选的addtivity属性。

  • name:指定受此logger约束的某个包或指定类
  • level:指定打印级别
  • additivity:是否向上级logger传递打印信息,默认true

2)设置root:只有一个level属性用于指定打印级别,默认debug

level和root之间的关系可以看作是java中的子类和父类,通过additivity属性进行继承


<logger name="com.whl.o2o" level="debug" additivity="true">
  <appender-ref ref="debugAppender"/>
  <appender-ref ref="infoAppender"/>
  <appender-ref ref="errorAppender"/>
logger>
	
<root level="info">
	
    <appender-ref ref="consoleAppender"/>
root>

2.3:过滤器
使用过滤器可以设置日志的传递.

  • onMatch表示匹配大于这个级别的日志
  • onMismatch表示匹配小于于这个级别的日志
    其属性值可以为:ACCEPT–打印
    DENY– 不打印
    NEUTRAL–中立
<filter class="ch.qos.logback.classic.filter.LevelFilter">
   
   <level>ERRORlevel>
   <onMatch>ACCEPTonMatch>
   <onMismatch>DENYonMismatch>
filter>

三,通过实例验证配置

3.1:logback.xml配置文件


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    

    
    <property name="log.level" value="debug" />
    <property name="log.maxHistory" value="30" />
    <property name="log.filePath" value="d:/tomcat9/logs/o2o_logs" />
    <property name="log.pattern"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
    
    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
    appender>
    
    <appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.filePath}/debug.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz
            fileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUGlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>
    
    <appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.filePath}/info.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz
            fileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>
    
    <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.filePath}/error.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz
            fileNamePattern>
            
            <maxHistory>${log.maxHistory}maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    <logger name="com.whl.o2o" level="${log.level}" additivity="true">
        <appender-ref ref="debugAppender"/>
        <appender-ref ref="infoAppender"/>
        <appender-ref ref="errorAppender"/>
    logger>
    <root level="info">
        <appender-ref ref="consoleAppender"/>
    root>
configuration>

3.2:测试类

public class BaseTest {
    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(BaseTest.class);
        //一般采用info记录方法的启动和结束
        logger.info("===start===");
        long startTime = System.currentTimeMillis();
        //一般在抛出的异常中打印error信息
        logger.error("test error");
        long endTime = System.currentTimeMillis();
        //一般debug用于记录用于调优的信息
        logger.debug("costTime:[{}ms]",endTime-startTime);
        logger.info("===end====");
    }
}

输出结果:
该日志输出格式为logback.xml中控制台日志输出设置指定的encoding格式

2019-07-30 17:41:43.033 [main] INFO com.whl.o2o.BaseTest - =start=
2019-07-30 17:41:43.039 [main] ERROR com.whl.o2o.BaseTest - test error
2019-07-30 17:41:43.045 [main] DEBUG com.whl.o2o.BaseTest - costTime:[6ms]
2019-07-30 17:41:43.053 [main] INFO com.whl.o2o.BaseTest - =end==

3.3:输出到配置文件的log
可以看到,通过logger和root之间的配置关系,我们不仅可以在指定的路径生成日志文本,也可以在控制台中进行日志的打印
logback配置详解_第2张图片

你可能感兴趣的:(logback,java,logback)