Log4j2配置自动清除指定日期前的日志

Log Archive Retention Policy: Delete on Rollover
Log4j-2.5 introduces a Delete action that gives users more control over what files are deleted at rollover time than what was possible with the DefaultRolloverStrategy max attribute. The Delete action lets users configure one or more conditions that select the files to delete relative to a base directory.
Note that it is possible to delete any file, not just rolled over log files, so use this action with care! With the testMode parameter you can test your configuration without accidentally deleting the wrong files.

当按照日期归档日志的时候,由于日志会一直存在,导致磁盘空间占用,如果服务器磁盘空间不足,且日志又没有长期保留的必要,可以直接在log4j2.xml中增加配置实现自动清理历史日志,Log4j2官方从2.5版本开始提供按照策略删除日期的功能。log4j2.xml样例配置如下:




    
        
            
        
        
            
                %d %p %c{1.} [%t] %m%n
            
            
            
                
                    
                    
                
            
        
    
    
        
        
            
        
    

关注其中的DefaultRolloverStrategy配置内容,按照此配置会自动清除Delete元素中指定basePath下超出30天的info-*.log.gz配置文件。由于是指定目录,指定文件名格式,指定日期范围,所以是可以清除非工程目录下的文件,所以一定不要配置错了。

你可能感兴趣的:(Java编程,java)