1 <?xml version="1.0" encoding="UTF-8"?>
2 <configuration>
3
4 <!--日志服务名-->
5 <property scope="local" name="SERVICE_NAME" value="demo"/>
6 <!--日志公共配置-->
7 <property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %X{TRACE_ID} [%thread] %-5level %logger[%L] - %m%n"/>
8 <property name="CHARSET" value="utf-8"/>
9 <property name="MAX_FILE_SIZE" value="10Kb"/>
10 <property name="MAX_HISTORY" value="1"/>
11 <property name="BASE_LOG_PATH" value="/data/logs"/>
12
13 <!--控制台打印日志-->
14 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
15 <encoder>
16 <pattern>${PATTERN}</pattern>
17 <charset>${CHARSET}</charset>
18 </encoder>
19 </appender>
20
21
22 <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
23 <!--当前日志文件名-->
24 <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.log</File>
25 <!--日志级别过滤-->
26 <filter class="com.luna.log.filter.LogbackInfoAndDebugFilter"></filter>
27 <!--日志滚动策略-->
28 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
29 <!--日志归档文件名-->
30 <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-info.%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
31 <!--按时间滚动,取决于fileNamePattern 归档日志的时间-->
32 <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
33 <!--单个日志文件最大,也会滚动-->
34 <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
35 </timeBasedFileNamingAndTriggeringPolicy>
36 <!--日志存活时间,大于这个时间的日志都将会删除-->
37 <maxHistory>${MAX_HISTORY}</maxHistory>
38 </rollingPolicy>
39 <encoder>
40 <!--日志格式-->
41 <pattern>${PATTERN}</pattern>
42 <!--日志编码-->
43 <charset>${CHARSET}</charset>
44 </encoder>
45 </appender>
46
47
48 <appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
49 <File>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.log</File>
50 <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
51 <level>ERROR</level>
52 </filter>
53 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
54 <fileNamePattern>${BASE_LOG_PATH}/${SERVICE_NAME}/${SERVICE_NAME}-error.%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
55 <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
56 <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
57 </timeBasedFileNamingAndTriggeringPolicy>
58 <maxHistory>${MAX_HISTORY}</maxHistory>
59 </rollingPolicy>
60 <encoder>
61 <pattern>${PATTERN}</pattern>
62 <charset>${CHARSET}</charset>
63 </encoder>
64 </appender>
65
66
67 <!--开启logback异步记录日志-->
68 <appender name="async-console" class="ch.qos.logback.classic.AsyncAppender">
69 <appender-ref ref="console"/>
70 <includeCallerData>true</includeCallerData>
71 </appender>
72
73 <appender name="async-fileAppender" class="ch.qos.logback.classic.AsyncAppender">
74 <appender-ref ref="fileAppender"/>
75 <includeCallerData>true</includeCallerData>
76 </appender>
77
78 <appender name="async-errorAppender" class="ch.qos.logback.classic.AsyncAppender">
79 <appender-ref ref="errorAppender"/>
80 <includeCallerData>true</includeCallerData>
81 </appender>
82
83
84 <!--整个项目打印INFO 以上级别-->
85 <logger name="com.luna" level="INFO">
86 <appender-ref ref="async-fileAppender"/>
87 <appender-ref ref="async-errorAppender"/>
88 </logger>
89
90 <!--记录druid-sql的记录-->
91 <logger name="druid.sql.Statement" level="debug" additivity="false">
92 <appender-ref ref="async-console"/>
93 <appender-ref ref="async-fileAppender"/>
94 </logger>
95
96 <root level="INFO">
97 <appender-ref ref="async-console"/>
98 </root>
99
100 </configuration>