Spring boot整合log4j2

以前整合过log4j2,但是今天再次整合发现都忘记了,而且也没有记下来

1.pom.xml中

  (1)把spring-boot-starter-web包下面的spring-boot-starter-logging排除


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

  说明:

    如果不先排除掉自带的log,会出现如下错误

    SLF4J: Class path contains multiple SLF4J bindings.

  (2)引入spring-boot-starter-log4j2包



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

2.添加log4j2配置文件

在src\main\resources添加log4j2-spring.xml





    

    
    
        
        
        
        
        
        
    

    

        
            
            
            
            
        

        
        <File name="Filelog" fileName="${FILE_PATH}/test.log" append="false">
            
        File>

        
        
            
            
                
                
            
            
            
                
                
                
            
            
            max="15"/>
        

        
        
            
            
                
                
            
            
            
                
                
                
            
            
            max="15"/>
        

        
        
            
            
            
            
                
                
                
            
            
            max="15"/>
        

    

    
    
    

        
        
        
            
        

        
            
            
            
            
            
        
    

Java程序代码中使用log4j2日志

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Logger logger = LoggerFactory.getLogger(WebMvcConfigurer.class);
logger.error("this is error test");
log.info("this is info test");

 启动后会出现log目录,同src在同一级

Spring boot整合log4j2_第1张图片

 

转载于:https://www.cnblogs.com/baby123/p/10831715.html

你可能感兴趣的:(Spring boot整合log4j2)