Creating log4j log file for each run with Date and timestamp

后端 未结 2 1145
粉色の甜心
粉色の甜心 2021-01-12 16:37

All, it seems like this question is posted multiple times but still i haven\'t got proper solution for my problem. I referred this and this but its not working.

As p

2条回答
  •  暖寄归人
    2021-01-12 16:43

    EDIT - removed the DailyFileAppender suggestion.

    You can create your own FileAppender, like this:

    public class NewFileOnRebootAppender extends FileAppender {
    
        public NewFileOnRebootAppender() {
        }
    
        @Override
        public void setFile(String file) {
            super.setFile(prependDate(file));
        }
    
        private static String prependDate(String filename) {
            return System.currentTimeMillis() + "_" + filename;
        }
    }
    

    And use it like this:

    log4j.appender.fileOnReboot=yourPackage.NewFileOnRebootAppender
    log4j.appender.fileOnReboot.File=appLogOnReboot.log
    log4j.appender.fileOnReboot.layout=org.apache.log4j.PatternLayout
    log4j.appender.fileOnReboot.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    

    The naming of the file is not perfect, but you get the idea..

提交回复
热议问题