当前位置:   article > 正文

重要通知:logback日志配置(亲测可用系列)_ch.qos.logback.classic.logger class is frozen

ch.qos.logback.classic.logger class is frozen

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

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

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

  1. <dependency>
  2. <groupId>org.slf4j</groupId>
  3. <artifactId>slf4j-api</artifactId>
  4. <version>1.7.21</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>ch.qos.logback</groupId>
  8. <artifactId>logback-core</artifactId>
  9. <version>1.2.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>ch.qos.logback</groupId>
  13. <artifactId>logback-classic</artifactId>
  14. <version>1.2.3</version>
  15. </dependency>

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

  1. <!--
  2. scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true
  3. scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
  4. debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false
  5. -->
  6. <configuration debug="true" scan="true" scanPeriod="60 seconds" packagingData="true">
  7. <!--
  8. property:用来定义变量值的标签,<property> 有两个属性,name和value。通过<property>定义的值会被插入到logger上下文中。定义变量后,可以使“${}”来使用变量。
  9. 例如使用<property>定义上下文名称,然后在<contentName>设置logger上下文时使用。
  10. -->
  11. <property name="contextName" value="context-name" />
  12. <property name="log_home" value="/tmp" />
  13. <!--
  14. contextName:每个logger都关联到logger上下文,默认上下文名称为“default”。
  15. 但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。
  16. -->
  17. <contextName>${contextName}</contextName>
  18. <!-- ******************** appender:日志目的地 start ******************** -->
  19. <!-- 标准输出:console -->
  20. <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  21. <!-- encoder 默认配置为PatternLayoutEncoder -->
  22. <encoder>
  23. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
  24. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
  25. </encoder>
  26. </appender>
  27. <!-- 文件输出:file -->
  28. <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
  29. <file>${log_home}/logback.log</file>
  30. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  31. <!--日志文件输出的文件名-->
  32. <FileNamePattern>${log_home}/logback.%d{yyyyMMddHHmm}.log</FileNamePattern>
  33. </rollingPolicy>
  34. <encoder>
  35. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
  36. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
  37. </encoder>
  38. <!--日志文件最大的大小-->
  39. <!--
  40. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  41. <MaxFileSize>10MB</MaxFileSize>
  42. </triggeringPolicy>
  43. -->
  44. </appender>
  45. <!-- ******************** appender:日志目的地 end ******************** -->
  46. <!-- ******************** logger:精确配置package或class的level、appender,可以配置0个或多个 end ******************** -->
  47. <!--
  48. level:日志级别(logger中的level会覆盖root中的,即以logger中为准,不管是否向上传递)
  49. name:包名 或 类的全路径
  50. addtivity:日志信息是否向上(root)传递
  51. -->
  52. <logger name="logback" />
  53. <!--logback.LogbackDemo:类的全路径 -->
  54. <logger name="logback.LogbackDemo" level="warn" additivity="false">
  55. <appender-ref ref="console"/>
  56. <appender-ref ref="file"/>
  57. </logger>
  58. <!-- ******************** logger:精确配置package或class日志的level、appender end ******************** -->
  59. <!-- ******************** root:定义日志的level、appender,且最多只能配置一个 start ******************** -->
  60. <!--TRACE < DEBUG < INFO < WARN < ERROR-->
  61. <root level="debug">
  62. <!-- 定义了INFO及以上级别的日志,分别在文件和控制台输出 -->
  63. <level value="debug" />
  64. <appender-ref ref="file" />
  65. <appender-ref ref="console" />
  66. </root>
  67. <!-- ******************** root:定义日志的level、appender,且最多只能配置一个 end ******************** -->
  68. </configuration>

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

  1. package logback;
  2. import ch.qos.logback.classic.Level;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. public class LogbackDemo {
  6. private static final Logger logger = LoggerFactory.getLogger(LogbackDemo.class);
  7. public static void main(String[] args) {
  8. //这里强制类型转换时为了能设置 logger 的 Level : TRACE < DEBUG < INFO < WARN < ERROR
  9. ch.qos.logback.classic.Logger logback_logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.foo");
  10. logback_logger.setLevel(Level.DEBUG);
  11. logback_logger.error("logback_logger.error");
  12. logback_logger.warn("logback_logger.warn");
  13. logback_logger.info("logback_logger.info");
  14. logback_logger.debug("logback_logger.debug");
  15. logback_logger.trace("logback_logger.trace");
  16. logger.error("logger.error");
  17. logger.warn("logger.warn");
  18. logger.info("logger.info");
  19. logger.debug("logger.debug");
  20. logger.trace("logger.trace");
  21. }
  22. }

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

  1. /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
  2. 18:48:21,521 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
  3. 18:48:21,521 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
  4. 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]
  5. 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]
  6. 18:48:21,776 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 1 minutes
  7. 18:48:21,781 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [context-name]
  8. 18:48:21,781 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
  9. 18:48:21,788 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
  10. 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
  11. 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]
  12. 18:48:21,862 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [file]
  13. 18:48:21,880 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@787387795 - No compression will be used
  14. 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
  15. 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'.
  16. 18:48:21,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over every minute.
  17. 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. 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
  19. 18:48:21,893 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - Active log file name: /tmp/logback.log
  20. 18:48:21,894 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file] - File property is set to [/tmp/logback.log]
  21. 18:48:21,895 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [logback.LogbackDemo] to WARN
  22. 18:48:21,895 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [logback.LogbackDemo] to false
  23. 18:48:21,895 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[logback.LogbackDemo]
  24. 18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file] to Logger[logback.LogbackDemo]
  25. 18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
  26. 18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - ROOT level set to DEBUG
  27. 18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [file] to Logger[ROOT]
  28. 18:48:21,896 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
  29. 18:48:21,896 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
  30. 18:48:21,897 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@45c8e616 - Registering current configuration as safe fallback point
  31. 18:48:21,901 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Elapsed period: Thu Apr 06 18:38:40 CST 2017
  32. 18:48:21,901 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [/tmp/logback.log] to [/tmp/logback.201704061838.log]
  33. 2017-04-06 18:48:21.900 [main] ERROR com.foo - logback_logger.error
  34. 2017-04-06 18:48:21.906 [main] WARN com.foo - logback_logger.warn
  35. 2017-04-06 18:48:21.907 [main] INFO com.foo - logback_logger.info
  36. 2017-04-06 18:48:21.907 [main] DEBUG com.foo - logback_logger.debug
  37. 2017-04-06 18:48:21.907 [main] ERROR logback.LogbackDemo - logger.error
  38. 2017-04-06 18:48:21.907 [main] WARN logback.LogbackDemo - logger.warn
  39. Process finished with exit code 0

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/595062
推荐阅读
相关标签
  

闽ICP备14008679号