Java日志记录框架Logback配置详解(企业级应用解决方案)

前言

Logback是现在比较流行的一个日志记录框架,它的配置比较简单学习成本相对较低,所以刚刚接触该框架的朋友不要畏惧,多花点耐心很快就能灵活应用了。本篇博文不会具体介绍Logback搭建过程,如果你是Logback初学者强烈建议阅读Logback常用配置详解,它对Logback的配置介绍的非常的详细,相信你看完这篇博客后会对Logback有一定的了解,然后再回头看下面的内容收获会更大

YAML配置

# 日志配置
logging:
  config: classpath:logback.xml

企业级应用常用Logback配置



<configuration scan="true" scanPeriod="3 seconds" debug="false">
    
    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        
        <encoder>
            <pattern>
                {"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS}","thread":"%t","line":"%line","log_level":"%p","class_name":"%C;","msg":"%m"}\n
            pattern>
        encoder>
    appender>

    
    <appender name="LOGBACK_ALL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <file>logs/logback-test-all.logfile>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <FileNamePattern>logs/logback-test.%d{yyyy-MM-dd}.log.zipFileNamePattern>
            
            
            
            <maxHistory>30maxHistory>
        rollingPolicy>

        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFOlevel>
        filter>

        <encoder>
            <pattern>
                {"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS}","thread":"%t","line":"%line","log_level":"%p","class_name":"%C;","msg":"%m%n", "caller":"%caller{1}"}
            pattern>
        encoder>
    appender>

    
    <appender name="LOGBACK_ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <file>logs/logback-test-error.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>logs/logback-test-error.log.%ifileNamePattern>
            <minIndex>1minIndex>
            <maxIndex>20maxIndex>
        rollingPolicy>

        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>50MBmaxFileSize>
        triggeringPolicy>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

        <encoder>
            <pattern>
                {"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS}","thread":"%t","line":"%line","log_level":"%p","class_name":"%C;","msg":"%m%n", "caller":"%caller{1}"}
            pattern>
        encoder>
    appender>

    
    <logger name="com.roberto" level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="LOGBACK_ALL_LOG" />
        <appender-ref ref="LOGBACK_ERROR_LOG" />
    logger>

    
    <root>
        <level value="INFO" />
        <appender-ref ref="STDOUT" />
        <appender-ref ref="LOGBACK_ALL_LOG" />
        <appender-ref ref="LOGBACK_ERROR_LOG" />
    root>
configuration>

以上配置部分涵盖了大部分日志记录的需求,如控制台输出日志、文件记录日志、日志输出格式化、按文件大小滚动日志、按日期滚动日志、日志级别过滤等等,在实际应用中只需对配置进行适当的修改即可。以上配置注释部分可以帮助你解决百分90的疑惑,接下来我以上诉配置为基准,运行程序进行简单解释说明

企业级应用常用Logback配置讲解

  • com.roberto包下测试

    package com.roberto;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LogbackTest {
      private static Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);
    
      public static void main(String[] args) {
          LOGGER.debug("=======DEBUG=======");
          LOGGER.info("========INFO=======");
          LOGGER.warn("========WARN=======");
          LOGGER.error("=======ERROR=======");
      }
    }

    以上诉配置为基准新建Logback测试类 观察日志输出情况 切记包名为com.roberto

    • 控制台输出
    {"timestamp":"2018-08-17T10:42:11.089","thread":"main","line":"28","log_level":"DEBUG","class_name":"com.roberto.LogbackTest;","msg":"=======DEBUG======="}
    {"timestamp":"2018-08-17T10:42:11.094","thread":"main","line":"29","log_level":"INFO","class_name":"com.roberto.LogbackTest;","msg":"========INFO======="}
    {"timestamp":"2018-08-17T10:42:11.095","thread":"main","line":"30","log_level":"WARN","class_name":"com.roberto.LogbackTest;","msg":"========WARN======="}
    {"timestamp":"2018-08-17T10:42:11.095","thread":"main","line":"31","log_level":"ERROR","class_name":"com.roberto.LogbackTest;","msg":"=======ERROR======="}
    name="com.roberto" level="DEBUG" additivity="false">
        ref ref="STDOUT" />
        ref ref="LOGBACK_ALL_LOG" />
        ref ref="LOGBACK_ERROR_LOG" />
    

    控制台打印以上内容是因为这里配置了包名为com.robertoDEBUG信息输出到控制台

    • logback-test-all.log内容
    {"timestamp":"2018-08-17T10:42:11.094","thread":"main","line":"29","log_level":"INFO","class_name":"com.roberto.LogbackTest;","msg":"========INFO=======", "caller":"Caller+0     at com.roberto.LogbackTest.main(LogbackTest.java:29)"}
    {"timestamp":"2018-08-17T10:42:11.095","thread":"main","line":"30","log_level":"WARN","class_name":"com.roberto.LogbackTest;","msg":"========WARN=======", "caller":"Caller+0     at com.roberto.LogbackTest.main(LogbackTest.java:30)"}
    {"timestamp":"2018-08-17T10:42:11.095","thread":"main","line":"31","log_level":"ERROR","class_name":"com.roberto.LogbackTest;","msg":"=======ERROR=======", "caller":"Caller+0     at com.roberto.LogbackTest.main(LogbackTest.java:31)"}
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFOlevel>
    filter>

    虽然配置com.robertoDEBUG级别信息输出到logback-test-all.log,但是由于LOGBACK_ALL_LOGappender中配置了过滤器,过滤掉了INFO级别以下的信息,所以logback-test-all.log信息如上

    • logback-test-error.log内容
    {"timestamp":"2018-08-17T10:42:11.095","thread":"main","line":"31","log_level":"ERROR","class_name":"com.roberto.LogbackTest;","msg":"=======ERROR=======", "caller":"Caller+0     at com.roberto.LogbackTest.main(LogbackTest.java:31)"}
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERRORlevel>
        <onMatch>ACCEPTonMatch>
        <onMismatch>DENYonMismatch>
    filter>

    虽然配置com.robertoDEBUG级别信息输出到logback-test-error.log,但是由于LOGBACK_ERROR_LOGappender中配置了过滤器,只输出ERROR级别信息,所以logback-test-error.log打印信息如上

  • 在非com.roberto包下测试

    package com.dreamt;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LogbackTest {
      private static Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);
    
      public static void main(String[] args) {
          LOGGER.debug("=======DEBUG=======");
          LOGGER.info("========INFO=======");
          LOGGER.warn("========WARN=======");
          LOGGER.error("=======ERROR=======");
      }
    }

    注意此处包名不为com.roberto,观察日志输出情况

    • 控制台输出
    {"timestamp":"2018-08-17T10:53:22.669","thread":"main","line":"29","log_level":"INFO","class_name":"com.dreamt.LogbackTest;","msg":"========INFO======="}
    {"timestamp":"2018-08-17T10:53:22.678","thread":"main","line":"30","log_level":"WARN","class_name":"com.dreamt.LogbackTest;","msg":"========WARN======="}
    {"timestamp":"2018-08-17T10:53:22.679","thread":"main","line":"31","log_level":"ERROR","class_name":"com.dreamt.LogbackTest;","msg":"=======ERROR======="}
    • logback-test-all.log内容
    {"timestamp":"2018-08-17T10:53:22.669","thread":"main","line":"29","log_level":"INFO","class_name":"com.dreamt.LogbackTest;","msg":"========INFO=======", "caller":"Caller+0     at com.dreamt.LogbackTest.main(LogbackTest.java:29)"}
    {"timestamp":"2018-08-17T10:53:22.678","thread":"main","line":"30","log_level":"WARN","class_name":"com.dreamt.LogbackTest;","msg":"========WARN=======", "caller":"Caller+0     at com.dreamt.LogbackTest.main(LogbackTest.java:30)"}
    {"timestamp":"2018-08-17T10:53:22.679","thread":"main","line":"31","log_level":"ERROR","class_name":"com.dreamt.LogbackTest;","msg":"=======ERROR=======", "caller":"Caller+0     at com.dreamt.LogbackTest.main(LogbackTest.java:31)"}
    • logback-test-error.log内容
    {"timestamp":"2018-08-17T10:53:22.679","thread":"main","line":"31","log_level":"ERROR","class_name":"com.dreamt.LogbackTest;","msg":"=======ERROR=======", "caller":"Caller+0     at com.dreamt.LogbackTest.main(LogbackTest.java:31)"}
    • 以上输出是由于配置文件如下
    
        value="INFO" />
        ref ref="STDOUT" />
        ref ref="LOGBACK_ALL_LOG" />
        ref ref="LOGBACK_ERROR_LOG" />
    

关于滚动日志这里就不做测试,你们可以自己写个循环输出日志进行测试,上面配置注释的非常清楚,只要你们肯动手肯定能实验成功的,以上部分纯属个人拙见,如有误解欢迎大家指正,谢谢~

你可能感兴趣的:(技术博客)