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

logback 设置不同包下的日志级别

程序员文章站 2022-07-03 16:23:42
...

1、实现效果:项目的整体的日志打印级别为ERROR,但在某个包下或某个类想打印INFO级别的日志。
2、配置:
FILE是ERROR级别日志打印;
SPECIAL 是INFO级别日志打印;
FILE与SPECIAL唯一不同是日志保存路径不同,其它策略相同;
通过 logger标签指定包路径或类路径并引用SPECIAL;

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <contextName>test</contextName>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.home}/logs/test/test.%d.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d %p (%file:%line\)- %m%n -[%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}]</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>


    <appender name="SPECIAL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.home}/logs/test/special/special.%d.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d %p (%file:%line\)- %m%n -[%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}]</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %p (%file:%line\)- %m%n </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <!--打印SQL-->
    <logger name="daoLog" level="ERROR" />
    <!-- 记录special操作日志 -->
    <logger name="aaa.bbb.ccc.DemoService" level="INFO" >
        <appender-ref ref="SPECIAL"/>
    </logger>

    <root level="error">
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>