重要通知:logback日志配置(亲测可用系列)

Logback是一款Java开源日志组件,出自log4j作者之手,在各种特性和性能上以全面超越log4j,Log4jConfigListener在Spring4中被标注为过时就是很好的证明。

1:咱们直接切入主题,实际操作下,怎样配置logback

step1:首先需要通过Maven在pom.xml中配置slf4j、logback依赖,至于slf4j和logback的关系这里不多说,请自行查阅相关文档,咱只注重快速使用,jar包版本可自行去maven下载最新系列!


            org.slf4j
            slf4j-api
            1.7.21
        
        
            ch.qos.logback
            logback-core
            1.2.3
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        

step2:resources中加入logback.xml,配置文件中已包含详细的注释



    
    
    

    
    ${contextName}


    

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

    
    
        ${log_home}/logback.log
        
            
            ${log_home}/logback.%d{yyyyMMddHHmm}.log
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        
        
        
    

    

    
    
    
    
    
        
        
    

    



    

    
    
        
        
        
        
    

    

step3:写一个测试类来验证一下

package logback;

import ch.qos.logback.classic.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogbackDemo {

    private static final Logger logger = LoggerFactory.getLogger(LogbackDemo.class);

    public static void main(String[] args) {

        //这里强制类型转换时为了能设置 logger 的 Level : TRACE < DEBUG < INFO <  WARN < ERROR
        ch.qos.logback.classic.Logger logback_logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.foo");
        logback_logger.setLevel(Level.DEBUG);

        logback_logger.error("logback_logger.error");
        logback_logger.warn("logback_logger.warn");
        logback_logger.info("logback_logger.info");
        logback_logger.debug("logback_logger.debug");
        logback_logger.trace("logback_logger.trace");

        logger.error("logger.error");
        logger.warn("logger.warn");
        logger.info("logger.info");
        logger.debug("logger.debug");
        logger.trace("logger.trace");
    }
}

step4:控制台输出如下,从前3条log可以发现,logback会自动去项目路径下依次查找logback-test.xml、logback.groovy、logback.xml

/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/bin/java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/tools.jar:/Users/aioria/IdeaProjects/springmvc/target/classes:/Users/aioria/.m2/repository/org/springframework/spring-core/4.3.0.RELEASE/spring-core-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/aioria/.m2/repository/org/springframework/spring-context/4.3.0.RELEASE/spring-context-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-aop/4.3.0.RELEASE/spring-aop-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-beans/4.3.0.RELEASE/spring-beans-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-expression/4.3.0.RELEASE/spring-expression-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-webmvc/4.3.0.RELEASE/spring-webmvc-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-web/4.3.0.RELEASE/spring-web-4.3.0.RELEASE.jar:/Users/aioria/.m2/repository/redis/clients/jedis/2.9.0/jedis-2.9.0.jar:/Users/aioria/.m2/repository/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar:/Users/aioria/.m2/repository/org/springframework/data/spring-data-redis/1.8.0.RELEASE/spring-data-redis-1.8.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/data/spring-data-keyvalue/1.2.0.RELEASE/spring-data-keyvalue-1.2.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/data/spring-data-commons/1.13.0.RELEASE/spring-data-commons-1.13.0.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-tx/4.3.6.RELEASE/spring-tx-4.3.6.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-oxm/4.3.6.RELEASE/spring-oxm-4.3.6.RELEASE.jar:/Users/aioria/.m2/repository/org/springframework/spring-context-support/4.3.6.RELEASE/spring-context-support-4.3.6.RELEASE.jar:/Users/aioria/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.22/jcl-over-slf4j-1.7.22.jar:/Users/aioria/.m2/repository/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar:/Users/aioria/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/aioria/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain logback.LogbackDemo
18:48:21,521 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
18:48:21,521 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
18:48:21,521 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/aioria/IdeaProjects/springmvc/target/classes/logback.xml]
18:48:21,776 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/Users/aioria/IdeaProjects/springmvc/target/classes/logback.xml] 
18:48:21,776 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 1 minutes
18:48:21,781 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [context-name]
18:48:21,781 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
18:48:21,788 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
18:48:21,801 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
18:48:21,852 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
18:48:21,862 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [file]
18:48:21,880 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@787387795 - No compression will be used
18:48:21,882 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@787387795 - Will use the pattern /tmp/logback.%d{yyyyMMddHHmm}.log for the active file
18:48:21,886 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyyMMddHHmm' from file name pattern '/tmp/logback.%d{yyyyMMddHHmm}.log'.
18:48:21,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over every minute.
18:48:21,890 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Apr 06 18:38:40 CST 2017
18:48:21,891 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
18:48:21,893 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - Active log file name: /tmp/logback.log
18:48:21,894 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - File property is set to [/tmp/logback.log]
18:48:21,895 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [logback.LogbackDemo] to WARN
18:48:21,895 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [logback.LogbackDemo] to false
18:48:21,895 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[logback.LogbackDemo]
18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file] to Logger[logback.LogbackDemo]
18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - ROOT level set to DEBUG
18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file] to Logger[ROOT]
18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
18:48:21,897 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@45c8e616 - Registering current configuration as safe fallback point
18:48:21,901 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Elapsed period: Thu Apr 06 18:38:40 CST 2017
18:48:21,901 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [/tmp/logback.log] to [/tmp/logback.201704061838.log]
2017-04-06 18:48:21.900 [main] ERROR com.foo - logback_logger.error
2017-04-06 18:48:21.906 [main] WARN  com.foo - logback_logger.warn
2017-04-06 18:48:21.907 [main] INFO  com.foo - logback_logger.info
2017-04-06 18:48:21.907 [main] DEBUG com.foo - logback_logger.debug
2017-04-06 18:48:21.907 [main] ERROR logback.LogbackDemo - logger.error
2017-04-06 18:48:21.907 [main] WARN  logback.LogbackDemo - logger.warn

Process finished with exit code 0

 

你可能感兴趣的:(java编程中的问题)