• The logback manual #01# Introduction


    依赖包如下pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.sample.logback</groupId>
        <artifactId>test-logback</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- -source 1.5 中不支持 try-with-resources-->
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.25</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>
    </project>

    例子程序:

    package org.sample.logback;
    
    import ch.qos.logback.classic.LoggerContext;
    import ch.qos.logback.core.util.StatusPrinter;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class LogbackTest {
    
        @Test
        public void testLogback() {
            Logger logger = LoggerFactory.getLogger(LogbackTest.class);
            logger.debug(logger.getName()); // 这个logger(记录器)名字叫org.sample.logback.LogbackTest
            logger.debug("Hello world."); // 一个级别为DEBUG的logging(记录)语句,并带有消息“Hello world”。
            // 不论项目大小,logging语句并不会有什么改变(总是像上面那样简单),只是配置不同!
    
            /*
            logback的默认配置策略:当未找到默认配置文件时,
            logback将把ConsoleAppender添加到root logger(根记录器).
            PS. appender是一种可以被视为“输出目的地”的类。所以上
            面这句话言下之意即:console将被作为root logger的输出目
            的地之一。
             */
    
            // 打印logback的内部状态,这依赖于具体的logback类,而不是slf4j API
            // 当然,在出现errors时,logback会自动打印内部状态而无需开启
            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
            StatusPrinter.print(lc); // 启用“打印logback内部状态”的模式,并不是只打印一次。这在诊断logback相关问题的时候非常有用!
        }
    }
    /*
    output=
    21:52:55.068 [main] DEBUG org.sample.logback.LogbackTest - org.sample.logback.LogbackTest
    21:52:55.068 [main] DEBUG org.sample.logback.LogbackTest - Hello world.
    21:52:55,037 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
    21:52:55,037 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
    21:52:55,037 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
    21:52:55,037 |-INFO in ch.qos.logback.classic.BasicConfigurator@3cd1f1c8 - Setting up default configuration.
     */
  • 相关阅读:
    论语心得
    水果总结
    欢乐颂
    大牌驾到
    Excel补全日期(日期按顺序补全)
    c语言define和typedef区别和使用
    c语言寄存器变量
    c语言伪常量const理解
    c语言静态断言-定义自己的静态断言
    c语言静态断言
  • 原文地址:https://www.cnblogs.com/xkxf/p/9958136.html
Copyright © 2020-2023  润新知