springBoot使用Log4j2实现日志输出

首先排除自带的log依赖,然后导入log4j2依赖

      
            org.springframework.boot
            spring-boot-starter
            
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
      
 
        
            org.springframework.boot
            spring-boot-starter-log4j2
        

在resources下创建log4j2-spring.xml



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

    
        
            
        
        
            
            
            
          
            
        
    

    
    
      
        
        
            
            
        
    


测试类

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LogTest {
    @Test
    public void test() {
        Logger log = LoggerFactory.getLogger(LogTest.class);
        log.warn("this is message {}", 1);
        Exception ex = new Exception("this is a message.");
        log.error("a new exeception", ex);
        log.trace("trace message.");
        log.info("info message.");
        for (int i = 0; i < 120000; i++)
            log.debug("debug message:{}={}", "line", i);
    }
}

参考:
https://www.cnblogs.com/keeya/p/10101547.html
https://www.cnblogs.com/songxingzhu/p/8867817.html

你可能感兴趣的:(springBoot使用Log4j2实现日志输出)