logback(1):搭建简单的logback框架

参考logback官方文档,地址为https://logback.qos.ch/manual/configuration.html;
一、用IntelliJ IDEA新建maven项目;
二、导入jar包:logback-classic-1.2.3.jar,logback-core-1.2.3.jar,slf4j-api-1.7.26.jar。pom.xml添加如下的dependency

  
      ch.qos.logback
      logback-core
      1.2.3
    

    
      ch.qos.logback
      logback-classic
      1.2.3
      test
    

    
        org.slf4j
        slf4j-api
       1.7.26
    

三、resources目录下新建logback-text.xml文件,内容如下:


    
           
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
           
   
   
         
   

四、java目录下新建java类:Foo.java,MyApp1.java

package chapters.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
      static final Logger logger = LoggerFactory.getLogger(Foo.class);
      public void doIt() {
          logger.debug("Did it again!");
     }
}

package chapters.configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyApp1 {
       final static Logger logger = LoggerFactory.getLogger(MyApp1.class);
       public static void main(String[] args) {
                logger.info("Entering application.");
                Foo foo = new Foo();
                foo.doIt();
                logger.info("Exiting application.");
         }
}

五、运行MyApp1.java,查看结果
logback(1):搭建简单的logback框架_第1张图片

你可能感兴趣的:(logback)