How to configure log4j to only keep log files for the last seven days?

前端 未结 14 1490
北海茫月
北海茫月 2020-12-02 14:01

I have the following logging problem with several Java applications using log4j for logging:

I want log files to be rotated daily, like

         


        
14条回答
  •  爱一瞬间的悲伤
    2020-12-02 14:54

    I create this Methode and call it by closing the application:

      public void deleteFiles(){
    
        File f = new File("log");
        File[] fileArray = f.listFiles();
        double timenow = System.currentTimeMillis();
    
        double olderTenDays = timenow - 864000000;// MS for ten days
    
        for (int i = 0; i < fileArray.length; i++) {
    
            if(fileArray[i].lastModified()< olderTenDays )
               fileArray[i].delete();
        }
     }
    

提交回复
热议问题