Filename with date in Log4j

后端 未结 3 1135
轮回少年
轮回少年 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:37

    you can manage this quickly and highly mantainable by just creating your own Appender.

    Just create a Class like this :

        import java.text.SimpleDateFormat;
        import java.util.Date;
        import org.apache.log4j.FileAppender;
    
        public class CustomFileAppender extends  FileAppender{
    
        @Override
        public void setFile(String fileName)
        {
            if (fileName.indexOf("%timestamp") >= 0) {
                Date d = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSS");
                fileName = fileName.replaceAll("%timestamp", format.format(d));
            }
            super.setFile(fileName);
       }
    }
    

    and place this in your properties:

       log4j.appender.file=com.portima.filenet.brio.ops.tools.CustomFileAppender
       log4j.appender.file.File=${log}/general.%timestamp.log
    

    Now you can give any type of filename you want.

提交回复
热议问题