log4j教程
log4j技术
log4j案例
Log4j2技术
log42j案例
Logback技术
Logback案例

logback案例2-控制台(过滤日志输出级别)

创建项目

编写日志配置

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                [logback-demo] %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5p] %c:%L - %m%n
            </pattern>
        </encoder>

        <!--
            ThresholdFilter:满足level以上的日志级别
        -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>warn</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root>
        <!--
            定义根日志输出最低日志级别,默认为debug
        -->
        <level value="info"/>
        <appender-ref ref="console"/>
    </root>

</configuration>

日志测试类

public class Demo {

    public static void main(String[] args) {
        //创建日志记录对象
        Logger logger = LoggerFactory.getLogger(Demo.class);

        logger.trace("This is TRACE message!");
        logger.debug("This is DEBGU message!");
        logger.info("This is INFO message!");
        logger.warn("This is WARN message!");
        logger.error("This is ERROR message!");
        
    }
}

查看测试结果