SpringBoot日志配置

SpringBoot默认使用logback作为日志框架,可在resources文件夹下添加logback.xml文件进行配置。

文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 工程名/项目名 -->
<contextName>project_name</contextName>

<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

<!--把>=debug的日志输出到控制台 -->
<appender name="SDTOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative %date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/info.log</file>
<rollingPolicy name="50M_FILE" class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/info.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>500</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>200MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative %date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/error.log</file>
<rollingPolicy name="50M_FILE" class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/error.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>100</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>200MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative %date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!-- 开发阶段使用DEBUG 生产环境使用INFO -->
 <root level="INFO">
<!--<root level="INFO">-->
<appender-ref ref="SDTOUT" />
  <appender-ref ref="INFO_FILE" />
<appender-ref ref="ERROR_FILE" />
 </root>

</configuration>