使用logback-spring.xml配置日志

完整配置如下(后面逐一分析):

logback-spring.xml





    
        
            
                
                %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
        
    

    
        
        
        
    


首先解析第一部分consoleLog:

<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>
            
            %d - %msg%n
        pattern>
    layout>
appender>

使用logback-spring.xml配置日志_第1张图片

要想这个配置起效果,就要启用这个appender,那么我们该如果启用这个appender呢?只需要如下配置即可

后面的也相同,都是在这样配置启用,后面就不多提了

<root level="info">
    <appender-ref ref="consoleLog" />
    <appender-ref ref="fileInfoLog" />
    <appender-ref ref="fileErrorLog" />
root>

使用logback-spring.xml配置日志_第2张图片

控制台的运行效果:

使用logback-spring.xml配置日志_第3张图片

现在我们解析第二部分fileInfoLog

 <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>

使用logback-spring.xml配置日志_第4张图片

最后是fileErrorLog

跟上面的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>

使用logback-spring.xml配置日志_第5张图片
最后纠错一下第二部分是有bug的

如果只想要输出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)]

这样就可以了!!!

最终版logback-spring.xml(bug修复):



<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>

你可能感兴趣的:(微信点餐系统,java,logback,slf4j,日志)