Log4j FileAppender recreating deleted files

后端 未结 5 1731
悲哀的现实
悲哀的现实 2021-01-03 07:08

I am using Log4j as logging framework in a project I am working on. I have the following situation: Log4j is configured to write the logs into a log file. At some point, thi

5条回答
  •  借酒劲吻你
    2021-01-03 07:36

    I study the source of log4j and find log4j can't create new log file, it just print the error message to system.err when the log file was deleted

        /** 
         This method determines if there is a sense in attempting to append. 
    
         

    It checks whether there is a set output target and also if there is a set layout. If these checks fail, then the boolean value false is returned. */ protected boolean checkEntryConditions() { if(this.closed) { LogLog.warn("Not allowed to write to a closed appender."); return false; } if(this.qw == null) { errorHandler.error("No output stream or file set for the appender named ["+ name+"]."); return false; } if(this.layout == null) { errorHandler.error("No layout set for the appender named ["+ name+"]."); return false; } return true; }

    I think there are two workaround

    1. create another cron thread to monitor the log file
    2. add judge in getLog or getInstance (singleton), check the log file does exist, if not then init log4j

提交回复
热议问题