Filename with date in Log4j

后端 未结 3 1130
轮回少年
轮回少年 2020-12-09 23:06

I\'m trying to append the current date to the log4j log file. So it would be something like this:

myApp-2011-01-07.log

The thing is that I do not want to us

3条回答
  •  眼角桃花
    2020-12-09 23:31

    I think you could just set a system property in code to contain the current date:

    static{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.setProperty("current.date", dateFormat.format(new Date()));
    }
    

    Then in your log4j.xml file you can use the system property when specifying the log file name in the appender:

    
        
    

    ETA: Now that I think about it you may have to setup the system property using a static initializer to make sure the property is set before log4j is configured.

提交回复
热议问题