当前位置:   article > 正文

logback配置详解(含示例源码)_groupid ch.qos.logback

groupid ch.qos.logback

本文主要讲述logback的详细配置

首先在pom.xml中引入loback依赖的jar包,代码如下所示:

  1. <dependency>
  2. <groupId>ch.qos.logback</groupId>
  3. <artifactId>logback-classic</artifactId>
  4. <version>1.0.13</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>ch.qos.logback</groupId>
  8. <artifactId>logback-core</artifactId>
  9. <version>1.0.13</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.slf4j</groupId>
  13. <artifactId>jcl-over-slf4j</artifactId>
  14. <version>1.7.7</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.slf4j</groupId>
  18. <artifactId>slf4j-api</artifactId>
  19. <version>1.7.7</version>
  20. </dependency>

一份自动回滚日志的配置文件,如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <!--消息日志路径配置-->
  4. <property name="LOG_HOME" value="D://logs"/>
  5. <!-- 控制台输出 -->
  6. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  7. <!-- 日志输出编码 -->
  8. <Encoding>UTF-8</Encoding>
  9. <layout class="ch.qos.logback.classic.PatternLayout">
  10. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  11. <pattern>
  12. %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
  13. </pattern>
  14. </layout>
  15. </appender>
  16. <!--*************************************记录监控日志*******************************************-->
  17. <appender name="monitorAppender"
  18. class="ch.qos.logback.core.rolling.RollingFileAppender">
  19. <Encoding>UTF-8</Encoding>
  20. <File>${LOG_HOME}/monitor.log</File>
  21. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  22. <!--日志文件输出的文件名 -->
  23. <FileNamePattern>${LOG_HOME}/file/monitor/monitor.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
  24. <MaxHistory>30</MaxHistory>
  25. <TimeBasedFileNamingAndTriggeringPolicy
  26. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  27. <MaxFileSize>2048MB</MaxFileSize>
  28. </TimeBasedFileNamingAndTriggeringPolicy>
  29. </rollingPolicy>
  30. <layout class="ch.qos.logback.classic.PatternLayout">
  31. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  32. <pattern>
  33. %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
  34. </pattern>
  35. </layout>
  36. </appender>
  37. <logger name="monitor" level="INFO">
  38. <appender-ref ref="monitorAppender"/>
  39. </logger>
  40. <!--*************************************记录bug日志*******************************************-->
  41. <appender name="bugAppender"
  42. class="ch.qos.logback.core.rolling.RollingFileAppender">
  43. <Encoding>UTF-8</Encoding>
  44. <File>${LOG_HOME}/bug.log</File>
  45. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  46. <!--日志文件输出的文件名 -->
  47. <FileNamePattern>${LOG_HOME}/file/bug/bug.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
  48. <MaxHistory>100</MaxHistory>
  49. <TimeBasedFileNamingAndTriggeringPolicy
  50. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  51. <MaxFileSize>2048MB</MaxFileSize>
  52. </TimeBasedFileNamingAndTriggeringPolicy>
  53. </rollingPolicy>
  54. <layout class="ch.qos.logback.classic.PatternLayout">
  55. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  56. <pattern>
  57. %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
  58. </pattern>
  59. </layout>
  60. </appender>
  61. <logger name="bug" level="INFO">
  62. <appender-ref ref="bugAppender"/>
  63. </logger>
  64. <!--*************************************记录错误日志*******************************************-->
  65. <appender name="errorAppender"
  66. class="ch.qos.logback.core.rolling.RollingFileAppender">
  67. <Encoding>UTF-8</Encoding>
  68. <File>${LOG_HOME}/error.log</File>
  69. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  70. <!--日志文件输出的文件名 -->
  71. <FileNamePattern>${LOG_HOME}/file/error/error.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
  72. <MaxHistory>30</MaxHistory>
  73. <TimeBasedFileNamingAndTriggeringPolicy
  74. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  75. <MaxFileSize>2048MB</MaxFileSize>
  76. </TimeBasedFileNamingAndTriggeringPolicy>
  77. </rollingPolicy>
  78. <layout class="ch.qos.logback.classic.PatternLayout">
  79. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  80. <pattern>
  81. %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
  82. </pattern>
  83. </layout>
  84. </appender>
  85. <logger name="error" level="INFO">
  86. <appender-ref ref="errorAppender"/>
  87. </logger>
  88. <!--*************************************记录消息到硬盘配置*******************************************-->
  89. <appender name="FILE"
  90. class="ch.qos.logback.core.rolling.RollingFileAppender">
  91. <Encoding>UTF-8</Encoding>
  92. <File>${LOG_HOME}/file.log</File>
  93. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  94. <!--日志文件输出的文件名 -->
  95. <FileNamePattern>${LOG_HOME}/file/file/file.log.%d{yyyy-MM-dd-HH}.log</FileNamePattern>
  96. <MaxHistory>30</MaxHistory>
  97. <TimeBasedFileNamingAndTriggeringPolicy
  98. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  99. <MaxFileSize>2048MB</MaxFileSize>
  100. </TimeBasedFileNamingAndTriggeringPolicy>
  101. </rollingPolicy>
  102. <layout class="ch.qos.logback.classic.PatternLayout">
  103. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  104. <pattern>
  105. %msg%n
  106. </pattern>
  107. </layout>
  108. </appender>
  109. <logger name="file" level="INFO">
  110. <appender-ref ref="FILE"/>
  111. </logger>
  112. <!-- 日志输出级别 -->
  113. <root level="INFO">
  114. <appender-ref ref="STDOUT"/>
  115. </root>
  116. </configuration>

以上配置文件,声明了四种类型的日志输入类型,并按照小时自动回滚日志。

如果想要查看详细的运行过程,请查看文章线程管理(二)获取和设置线程信息(含示例源码)中的源码,运行查看。



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

闽ICP备14008679号