logback日志打印过滤
1.只打印sql语句以及参数
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration scan = "true" scanPeriod = "60 seconds" debug = "false" > <!-- 设置 logger context 名称,一旦设置不可改变,默认为default --> <!-- <contextName>myAppName</contextName> --> < timestamp key = "bySecond" datePattern = "yyyyMMdd'T'HHmmss" /> <!-- 日志文件大小最大值 --> < property name = "maxFileSize" value = "1kb" /> <!-- LoggerContextListener 接口的实例能监听 logger context 上发生的事件,比如说日志级别的变化 --> < contextListener class = "ch.qos.logback.classic.jul.LevelChangePropagator" > < resetJUL >true</ resetJUL > </ contextListener > < appender name = "STDOUT" class = "ch.qos.logback.core.ConsoleAppender" > <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> < filter class = "ch.qos.logback.core.filter.EvaluatorFilter" > < evaluator > <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator --> < expression >return message.contains("mapper") || message.contains("component") || message.contains("MapperFactoryBean");</ expression > </ evaluator > < OnMismatch >NEUTRAL</ OnMismatch > < OnMatch >DENY</ OnMatch > </ filter > < filter class = "ch.qos.logback.core.filter.EvaluatorFilter" > < evaluator > <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator --> < expression >return message.contains("Preparing") || message.contains("Parameters:") || message.contains("Total:");</ expression > </ evaluator > < OnMismatch >DENY</ OnMismatch > < OnMatch >ACCEPT</ OnMatch > </ filter > < encoder > < pattern >%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </ pattern > </ encoder > </ appender > < logger name = "java.sql.Statement" level = "DEBUG" /> < logger name = "java.sql.PreparedStatement" level = "DEBUG" /> < logger name = "com.xxx" level = "debug" additivity = "false" > < appender-ref ref = "STDOUT" /> </ logger > < root > < appender-ref ref = "STDOUT" /> </ root > </ configuration > |
上面有两个过滤器,
其中第一个过滤器是过滤掉所有包含字符串“mapper”,“component”,“MapperFactoryBean”的日志打印
第二个过滤器是只打印包含字符串”Preparing”,”Paramters:”,”Total:”的日志内容。
logback.xml日志文件过滤器配置简析
自定义过滤器
java代码,目的是在mdc中放值,然后在日志中打印
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
|
package com.controller; import com.pojo.JsonData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping (path = "/api/log/" ) public class LoggerController { Logger logger= LoggerFactory.getLogger(getClass()); @RequestMapping (path = "test1" ) public Object logTest1(){ logger.trace( "-----trace级别日志-----" ); logger.debug( "-----debug级别日志-----" ); logger.info( "-----info级别日志-----" ); logger.warn( "-----warn级别日志-----" ); logger.error( "-----error级别日志-----" ); return JsonData.buildSuccess(); } @RequestMapping (path = "test2" ) public Object logTest2(){ MDC.put( "name" , "yangche" ); MDC.put( "age" , "25" ); MDC.put( "like" , "wujing" ); logger.trace( "-----trace级别日志-----" ); logger.debug( "-----debug级别日志-----" ); logger.info( "-----info级别日志-----" ); logger.warn( "-----warn级别日志-----" ); logger.error( "-----error级别日志-----" ); logger.info( "" ); MDC.clear(); return JsonData.buildSuccess(); } } |
写一个过滤器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.filter; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.filter.Filter; import ch.qos.logback.core.spi.FilterReply; import org.springframework.util.StringUtils; public class SimpleLogFilter extends Filter<ILoggingEvent> { @Override public FilterReply decide(ILoggingEvent event) { System.err.println(event.getMDCPropertyMap().get( "name" )); if (StringUtils.isEmpty(event.getMdc().get( "name" ))){ return FilterReply.DENY; } else { return FilterReply.NEUTRAL; } } } |
以上过滤器要继承Filter<ILoggingEvent>,在变量event中可以取到mdc的数据,msg,等等等等,可以用java对日志进行自定义的判断,过滤不需要的过滤器,对日志内容进行替换等等,随心所欲。
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
60
61
62
63
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > <!--控制台日志--> < appender name = "consoleApp" class = "ch.qos.logback.core.ConsoleAppender" > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern > %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n </ pattern > </ layout > </ appender > <!--ERROR级别日志--> < appender name = "fileErrorApp" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < filter class = "ch.qos.logback.classic.filter.ThresholdFilter" > < level >ERROR</ level > </ filter > < encoder > < pattern > %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n </ pattern > </ encoder > <!-- 设置滚动策略 --> < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!-- 路径 --> < fileNamePattern >log/app.err.%d.log</ fileNamePattern > <!-- 控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动, 且<maxHistory> 是1,则只保存最近1个月的文件,删除之前的旧文件 --> < MaxHistory >1</ MaxHistory > </ rollingPolicy > </ appender > <!--INFO级别日志--> < appender name = "fileInfoApp" class = "ch.qos.logback.core.rolling.RollingFileAppender" > <!--<filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <!– onMatch="ACCEPT" 表示匹配该级别及以上 onMatch="DENY" 表示不匹配该级别及以上 onMatch="NEUTRAL" 表示该级别及以上的,由下一个filter处理,如果当前是最后一个,则表示匹配该级别及以上 onMismatch="ACCEPT" 表示匹配该级别以下 onMismatch="NEUTRAL" 表示该级别及以下的,由下一个filter处理,如果当前是最后一个,则不匹配该级别以下的 onMismatch="DENY" 表示不匹配该级别以下的 –> <onMatch>ACCEPT</onMatch> <!–<onMismatch>DENY</onMismatch>–> </filter>--> < filter class = "com.filter.SimpleLogFilter" /> <!--我写的过滤器,过滤的是mdc中name的值为空的日志内容,所以所有name为空值的不会被打印--> < encoder > < pattern > %X{name} %X{age} %X{like}-----%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n </ pattern > </ encoder > <!-- 滚动策略 --> < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!-- 路径 --> < fileNamePattern >log/app.info.%d.log</ fileNamePattern > </ rollingPolicy > </ appender > <!--root level最低级别--> < root level = "INFO" > < appender-ref ref = "consoleApp" /> < appender-ref ref = "fileInfoApp" /> < appender-ref ref = "fileErrorApp" /> </ root > </ configuration > |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_26917447/article/details/81011315