How do you get log4j to roll files based on date and size?

后端 未结 6 600
囚心锁ツ
囚心锁ツ 2020-12-02 17:11

So log4j comes with two existing log rollers: RollingFileAppender, and DailyRollingFileAppender. Has anyone heard of an appender that does both of what the former do?

<
6条回答
  •  心在旅途
    2020-12-02 18:02

    Too late to reply. But hopefully this will help someone.

    Add log4j dependency to your pom.xml file

       
            log4j
            log4j
            1.2.17
        
    

    Grab a copy of this FileAppender implementation. Thanks to the author of this file.

    http://haobangshou.googlecode.com/svn/trunk/hbs/APPLICATION/server/common/src/com/hbs/common/appender/TimeSizeRollingFileAppender.java
    

    Place this log4j.xml file in your resources folder

    
    
    
    
        
            
        
     
    
    
        
        
        
        
        
        
        
            
        
    
    
    
    
        
        
        
    
    
    
    
        
        
        
    
    
    
        
        
        
    
    
    
        
        
        
     
    

    To do a quick test:

    import org.apache.log4j.Logger;
    
    public class Main {
    
        static public void main(String[] args) {
            Logger log = Logger.getLogger(Main.class);
            for(int i = 0; i < 10000; i ++)
                log.info("Testing log");
            }
    }
    

    Done!!

提交回复
热议问题