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