springboot集成logback日志 通用logback.xml模板详解

先看Spring Boot中依赖的logback,log4j,slf4j相关Jar包

springboot集成logback日志 通用logback.xml模板详解_第1张图片

1.最简单的默认打印控制台日志 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImportDemoController {
Logger logger= LoggerFactory.getLogger(ImportDemoController.class);
    @Autowired
    private User user;

    @Autowired
    private ImportDemo importDemo;

    @Autowired
    private User01 user01;

    @Autowired
    private UserFactoryBean userFactoryBean;

    @Autowired
    private UserLazy userLazy;

    @RequestMapping("/importDemo")
    public String demo() throws Exception {
        logger.info("ImportDemoController 开始>>>");
        importDemo.doSomething();
        user01.username = "user01";
        String s = user.username;
        String s1 = user01.username;
        String s4 = userFactoryBean.getObject().username;
        String s5=userLazy.username;
        logger.info("ImportDemoController 结束<<<");
        return "ImportDemo@SpringBoot " + s + " " + s1 + " " + s4+  " " + s5;

    }
}

注意需要引入:org.slf4j相关包。

 

2.修改日志级别

日志级别 trace

默认级别为info,默认打印info及其以上级别的日志,默认不打印debug日志,

public String demo() throws Exception {
        logger.debug("ImportDemoController 开始>>>");
        importDemo.doSomething();
        user01.username = "user01";
        String s = user.username;
        String s1 = user01.username;
        String s4 = userFactoryBean.getObject().username;
        String s5=userLazy.username;
        logger.debug("ImportDemoController 结束<<<");
        return "ImportDemo@SpringBoot " + s + " " + s1 + " " + s4+  " " + s5;

    }

修改Spring Boot配置文件即可打印debug日志

application.properties

logging.level.root=debug

logging.level设置日志级别,后面跟生效的区域,比如root表示整个项目,也可以设置为某个包下,也可以具体到某个类名(日志级别的值不区分大小写) https://www.cnblogs.com/yanguobin/p/11487667.html

logging.level.root=info就是默认的情况,你改为我上面那样就打印出debug了,开发环境打印,线上是不允许的。

springboot集成logback日志 通用logback.xml模板详解_第2张图片

 

3.使用logback.xml配置文件实现更精细的配置

logback.xml和application.properties放在同一层目录即可,这个logback.xml非常好,源自网络,只要简单修改存储路径和对应的包空间就可以直接在项目中使用了




    
    
    
    
    

    
    
        
        
            ${log.pattern}
        
    

    
    
    
        

        
        ${log.filePath}/debug.log
        
        
            
            ${log.filePath}/debug/debug-%d{yyyy-MM-dd}.log.gz
            
            
            ${log.maxHistory}
        
        
        
            ${log.pattern}
        
        
        
            debug
            ACCEPT
            
            DENY
        
    

    
    

        
        ${log.filePath}/info.log
        
        
            
            ${log.filePath}/info/info-%d{yyyy-MM-dd}.log.gz
            
            
            ${log.maxHistory}
        
        
        
            ${log.pattern}
        
        
        
            info
            ACCEPT
            
            DENY
        
    

    
    

        
        ${log.filePath}/error.log
        
        
            
            ${log.filePath}/error/error-%d{yyyy-MM-dd}.log.gz
            
            
            ${log.maxHistory}
        
        
        
            ${log.pattern}
        
        
        
            error
            ACCEPT
            
            DENY
        
    

    
    
        
        
        
        
        
        
    

    
    
    
        
        
    

我的配置是写在D盘log目录下,可以根据需要设置。 

springboot集成logback日志 通用logback.xml模板详解_第3张图片

你可能感兴趣的:(SpringBoot)