欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义lockback拦截配置

程序员文章站 2022-03-05 08:16:59
...

方法一:需要配置拦截字符

1.在 <evaluator>子标签下,  message.contains("response :"),contains里面就是配置需要拦截的字符串。如果当行里面有xxxx,就拦截下来

<!-- Configure the monthly calculator log  -->
		<appender name="JSONOUT" class="ch.qos.logback.core.rolling.RollingFileAppender">   
				<filter class="ch.qos.logback.core.filter.EvaluatorFilter">         
					  <evaluator>  
							<expression>return message.contains("response :");</expression>   
					  </evaluator>   
					  <OnMatch>NEUTRAL</OnMatch>  
					  <OnMismatch>DENY</OnMismatch>  
				</filter>  
				<file>H:/Java/workspace_one/OCR_Server/logs/ocr_server_json_result.log</file>

                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                        <!-- Daily rollover -->
                        <fileNamePattern>H:/Java/workspace_one/OCR_Server/logs/ocr_server_json_result.log.%d{yyyy-MM-dd}.log
                        </fileNamePattern> 
				
				        <!-- Keep 180 days' worth of history -->
                        <maxHistory>180</maxHistory>
				 </rollingPolicy>
				 
				<encoder>   
				  <pattern>   
					%d{HH:mm:ss.SSS} %-5level [%thread] %logger - %msg%n  
				  </pattern>   
				</encoder>   
		</appender>

 2.最后还不要忘记添加

<appender-ref ref="JSONOUT" />

 

3.不输出一些框架信息

把不想输出的包路径写进去

<logger name="org.springframework" level="info" />  

 不输出info级别,只输出debug级别的:

<!-- Root Logger -->
        <root>
                <priority value="debug" />
                <appender-ref ref="console" />
                <appender-ref ref="file" />
        </root>

 

4.依赖包:

   compile 'org.slf4j:slf4j-api:1.7.12'
 	compile 'org.slf4j:jcl-over-slf4j:1.7.12'
	compile 'ch.qos.logback:logback-classic:1.1.3'
	
	compile 'org.codehaus.janino:janino:3.0.0'
	compile 'org.codehaus.janino:commons-compiler:3.0.0'

 

Ref:

http://logback.qos.ch/manual/filters.html

 

方法二:分文件

1.

<appender name="REGULAR_EXP" class="ch.qos.logback.core.rolling.RollingFileAppender">
				<file>{path}/regular_exp_result.log</file>

				<encoder>
					<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level-%msg%n</pattern>
				</encoder>

				<filter class="ch.qos.logback.classic.filter.LevelFilter">
					<level>ERROR</level>
				</filter>

			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
					<!-- Daily rollover -->
					<fileNamePattern>{path}/regular_exp_result.%d{yyyy-MM-dd}.log
					</fileNamePattern>

					<!-- Keep 180 days' worth of history -->
					<maxHistory>180</maxHistory>
			</rollingPolicy>

			<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
				<maxFileSize>50KB</maxFileSize>
			</triggeringPolicy>
		</appender>

 2.

<logger name="REGULAR_EXP" level="DEBUG">
        	<appender-ref ref="REGULAR_EXP" />
    	</logger>

 3.new logger 使用

private static final Logger regExLogger = LoggerFactory.getLogger("REGULAR_EXP");

 

相关标签: logback