You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # spring-boot-demo-logback
  2. > 此 demo 主要演示了如何使用 logback 记录程序运行过程中的日志,以及如何配置 logback,可以同时生成控制台日志和文件日志记录,文件日志以日期和大小进行拆分生成。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-logback</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-logback</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <optional>true</optional>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <finalName>spring-boot-demo-logback</finalName>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>
  50. ```
  51. ## SpringBootDemoLogbackApplication.java
  52. ```java
  53. /**
  54. * <p>
  55. * 启动类
  56. * </p>
  57. *
  58. * @package: com.xkcoding.logback
  59. * @description: 启动类
  60. * @author: yangkai.shen
  61. * @date: Created in 2018/9/30 11:16 PM
  62. * @copyright: Copyright (c) 2018
  63. * @version: V1.0
  64. * @modified: yangkai.shen
  65. */
  66. @SpringBootApplication
  67. @Slf4j
  68. public class SpringBootDemoLogbackApplication {
  69. public static void main(String[] args) {
  70. ConfigurableApplicationContext context = SpringApplication.run(SpringBootDemoLogbackApplication.class, args);
  71. int length = context.getBeanDefinitionNames().length;
  72. log.trace("Spring boot启动初始化了 {} 个 Bean", length);
  73. log.debug("Spring boot启动初始化了 {} 个 Bean", length);
  74. log.info("Spring boot启动初始化了 {} 个 Bean", length);
  75. log.warn("Spring boot启动初始化了 {} 个 Bean", length);
  76. log.error("Spring boot启动初始化了 {} 个 Bean", length);
  77. try {
  78. int i = 0;
  79. int j = 1 / i;
  80. } catch (Exception e) {
  81. log.error("【SpringBootDemoLogbackApplication】启动异常:", e);
  82. }
  83. }
  84. }
  85. ```
  86. ## logback-spring.xml
  87. ```xml
  88. <?xml version="1.0" encoding="UTF-8"?>
  89. <configuration>
  90. <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
  91. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  92. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  93. <level>INFO</level>
  94. </filter>
  95. <encoder>
  96. <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
  97. <charset>UTF-8</charset>
  98. </encoder>
  99. </appender>
  100. <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
  101. <!--如果只是想要 Info 级别的日志,只是过滤 info 还是会输出 Error 日志,因为 Error 的级别高, 所以我们使用下面的策略,可以避免输出 Error 的日志-->
  102. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  103. <!--过滤 Error-->
  104. <level>ERROR</level>
  105. <!--匹配到就禁止-->
  106. <onMatch>DENY</onMatch>
  107. <!--没有匹配到就允许-->
  108. <onMismatch>ACCEPT</onMismatch>
  109. </filter>
  110. <!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。-->
  111. <!--<File>logs/info.spring-boot-demo-logback.log</File>-->
  112. <!--滚动策略,按照时间滚动 TimeBasedRollingPolicy-->
  113. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  114. <!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
  115. <FileNamePattern>logs/spring-boot-demo-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern>
  116. <!--只保留最近90天的日志-->
  117. <maxHistory>90</maxHistory>
  118. <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志-->
  119. <!--<totalSizeCap>1GB</totalSizeCap>-->
  120. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  121. <!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
  122. <maxFileSize>2MB</maxFileSize>
  123. </timeBasedFileNamingAndTriggeringPolicy>
  124. </rollingPolicy>
  125. <!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
  126. <!--<maxFileSize>1KB</maxFileSize>-->
  127. <!--</triggeringPolicy>-->
  128. <encoder>
  129. <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
  130. <charset>UTF-8</charset> <!-- 此处设置字符集 -->
  131. </encoder>
  132. </appender>
  133. <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
  134. <!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter-->
  135. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  136. <level>Error</level>
  137. </filter>
  138. <!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。-->
  139. <!--<File>logs/error.spring-boot-demo-logback.log</File>-->
  140. <!--滚动策略,按照时间滚动 TimeBasedRollingPolicy-->
  141. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  142. <!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间-->
  143. <FileNamePattern>logs/spring-boot-demo-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern>
  144. <!--只保留最近90天的日志-->
  145. <maxHistory>90</maxHistory>
  146. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  147. <!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 -->
  148. <maxFileSize>2MB</maxFileSize>
  149. </timeBasedFileNamingAndTriggeringPolicy>
  150. </rollingPolicy>
  151. <encoder>
  152. <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
  153. <charset>UTF-8</charset> <!-- 此处设置字符集 -->
  154. </encoder>
  155. </appender>
  156. <root level="info">
  157. <appender-ref ref="CONSOLE"/>
  158. <appender-ref ref="FILE_INFO"/>
  159. <appender-ref ref="FILE_ERROR"/>
  160. </root>
  161. </configuration>
  162. ```

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。