springboot+mybatis+logback日志配置及使用详解

1、application.properties

#端口
server.port=8082  
# 项目路径
server.servlet.context-path=/test
#logback配置,与application.properties同一级目录
logging.config=classpath:logback.xml
#mybatis配置
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.type-aliases-package=com.demo.bi.domain

2、logback.xml



    demo
    

    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    

    
    
        ${LOG_PATH}/log_error.log
        
        
            
            ${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}.%i.log
            
            
                10MB
            
        
        
        true
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            utf-8
        
        
        
            error
            ACCEPT
            DENY
        
    

    
    
        ${LOG_PATH}/log_info.log
        
            ${LOG_PATH}/info/log-info-%d{yyyy-MM-dd}.%i.log
            
                10MB
            
        
        true
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            utf-8
        
        
            info
            ACCEPT
            DENY
        
    

    
    
        ${LOG_PATH}/log_debug.log
        
            ${LOG_PATH}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
            
                2MB
            
        
        true
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            utf-8
        
        
            DEBUG
            ACCEPT
            DENY
        
    

    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
        
        
        
        
        
    

3、LoggerTest.java

package com.demo.bi.log;

import com.demo.bi.BlackholebiApplication;
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;

/**
 *  功能描述: 日志测试
 *
 * @author LT
 * @create 2019/4/1
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class LoggerTest {

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

    @Test
    public void testLogger(){
        logger.info("info日志记录测试--------------------------");
        logger.error("error日志记录测试--------------------------");
        logger.debug("error日志记录测试--------------------------");
        try {

            System.out.println(3/0);

        }catch (Exception e){
            logger.error("exception error -------------",e);
        }
    }

}

你可能感兴趣的:(JAVA工程师)