SpringBoot2.0学习之整合logback输出日志

SpringBoot2.0学习之整合logback输出日志

ps: 由于SpringBoot默认整合的就是logback,所以相关的依赖是不需要引入的。spring-boot-starter包下已经把相关依赖引入好了。所以我们只需要加入logback相关的配置就可以了。

1. 在resources下创建logback.xml文件

内容如下:




    
    
    
    

    
    
        
            ${pattern}
            ${charsetEncoding}
        
    
    
    
        
        ${logPath}/infoLog.log
        
        
            
            ${logPath}/infoLog.%d{yyyy-MM-dd}.log
            
            30
            
            
                
            
        
        
        true
        
            ${pattern}
            ${charsetEncoding}
        
    
    
    
        
        ${logPath}/errorLog.log
        
        
            
            ${logPath}/errorLog.%d{yyyy-MM-dd}.log
            
            30
            
            
            
            
        
        
        true
        
            ${pattern}
            ${charsetEncoding}
        
        
            
            ERROR
        
    
    
    
        
        
        
    

测试代码:

package com.qf.marklife;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {

    private final static Logger logger = LoggerFactory.getLogger(Test.class);

    @Test
    public void test(){
        logger.info("成功输出info级别日志");
        logger.error("成功输出error级别日志");
    }
}

测试结果

控制台输出:

在这里插入图片描述

文件输出:

error:在这里插入图片描述
info:
SpringBoot2.0学习之整合logback输出日志_第1张图片

logback详细学习传送门:https://www.cnblogs.com/lixuwu/p/5804793.html


人生是一场永不落幕的演出,我们每一个人都是演员,只不过,有的人顺从自己,有的人取悦观众。

你可能感兴趣的:(springboot2.0学习)