Log4j does not recreate files on deletion

前端 未结 5 2120
面向向阳花
面向向阳花 2020-12-19 12:33

I have a web application in Tomcat that uses log4j for logging.
If I delete the log files while the web application is running the files are not recreated?
How can I

5条回答
  •  甜味超标
    2020-12-19 13:14

    You can have a customized RollingFileAppender and check for file existence. This code checks for file existence before every logging and creates the file if it is missing.

    public class CustomRollingFileAppender extends RollingFileAppender {
    
    // ... Constructor
    
    @Override
        public void append(LoggingEvent event) {
            if (!new File(this.fileName).exists()) {
                try {
                    setFile(this.fileName, fileAppend, bufferedIO, bufferSize);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            super.append(event);
        }
    }
    

提交回复
热议问题