%d - %msg%n
ERROR
DENY
ACCEPT
%msg%n
F:/project/sell/log/info.%d.log
ERROR
%msg%n
F:/project/sell/log/error.%d.log
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
pattern>
layout>
appender>
后面的也相同,都是在这样配置启用,后面就不多提了
<root level="info">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
root>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>DENYonMatch>
<onMismatch>ACCEPTonMismatch>
filter>
<encoder>
<pattern>
%msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>F:/project/sell/log/info.%d.logfileNamePattern>
rollingPolicy>
appender>
跟上面的fileInfoLog大同小异,滚动策略都是一样的,只是改了一个后面的文件名称,改了过滤器的类
上面用的是ch.qos.logback.classic.filter.LevelFilter
这个类
而这里用的是ch.qos.logback.classic.filter.ThresholdFilter
这个类
那有人会问了,这两个类有什么区别呢?
ThresholdFilter这个类,是只输出指定级别的日志
而LevelFilter这个类,要配合onMatch标签,如果该标签中是DENY则,说明拦截该标签级别,onMismatch标签是ACCEPT,则说明,没抓取到上面lever级别的日志,就accept,即放行.
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>F:/project/sell/log/error.%d.logfileNamePattern>
rollingPolicy>
appender>
如果只想要输出info级别的日志,像上面那样配置,是只过滤了error级别的日志信息,warn级别的日志并没有过滤掉,我这边做一下修改(后面发现的,现在做一下补充修改)
##fileInfoLog
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>F:/project/sell/log/info.%d.logfileNamePattern>
rollingPolicy>
appender>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YNY31LXQ-1602166332044)(C:\Users\Carlos\AppData\Roaming\Typora\typora-user-images\image-20201008215924077.png)]
这样就可以了!!!
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
pattern>
layout>
appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>F:/project/sell/log/info.%d.logfileNamePattern>
rollingPolicy>
appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>
%d - %msg%n
pattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>F:/project/sell/log/error.%d.logfileNamePattern>
rollingPolicy>
appender>
<root level="info">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
root>
configuration>