Log4j FileAppender recreating deleted files

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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, this log file is copied to another destination and deleted. Logging framework keeps working, but the logs are not written to the log file because it is deleted. Is there any way to tell Log4j to recreate the file and keep writing the logs into the log file.

Best regards, Rashid

回答1:

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.        <p>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 <code>false</code> 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


回答2:

Make sure you can declare this line in your log4j file

    log4j.appender.rollingFile.File=D:/myapp/mylog.log 

If you already declared it, you log file can delete or replace as you like. Then you rerun your program and new log file is created in this path.



回答3:

Try This Class

package wodong.test; import java.io.File; import java.io.IOException;  import org.apache.log4j.FileAppender; import org.apache.log4j.spi.LoggingEvent;  public class LastFileAppender extends FileAppender {     @Override     public void append(LoggingEvent event) {         checkLogFileExist();         super.append(event);     }     private void checkLogFileExist(){         File logFile = new File(super.fileName);         if (!logFile.exists()) {             try {                 logFile.createNewFile();             } catch (IOException e) {                 System.out.println("Error while create new log file.");             }         }     } } 

Also edit the log4j config file

log4j.appender.R=wodong.test.LastFileAppender 


回答4:

Try This one. I do not have a Linux machine right now, so I'm not sure if it can resolve the performance issue.

package wodong.test; import java.io.File; import java.io.IOException;  import org.apache.log4j.FileAppender; import org.apache.log4j.spi.LoggingEvent;  public class LastFileAppender extends FileAppender {     @Override     public void append(LoggingEvent event) {          checkLogFileExist();         super.append(event);     }      private void checkLogFileExist() {         if (qw == null) {             File logFile = new File(super.fileName);             if (!logFile.exists()) {                 try {                     logFile.createNewFile();                 } catch (IOException e) {                     System.out.println("Error while create new log file.");                 }             }         }     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!