One logfile per run with log4j

后端 未结 4 1244
梦谈多话
梦谈多话 2020-12-03 11:26

How do you configure log4j.properties to have exactly one logfile per run of an app. I\'ve read that you should use a timestamp in the filename but that will create many fil

4条回答
  •  醉酒成梦
    2020-12-03 11:42

    I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution. log4j.properties:

    # Root logger option
    log4j.rootLogger=INFO, fileout
    
    # Direct log messages to file
    log4j.appender.fileout=org.apache.log4j.FileAppender
    log4j.appender.fileout.File=/logs/myapp_${current.date}.log
    log4j.appender.fileout.ImmediateFlush=true
    log4j.appender.fileout.Threshold=debug
    log4j.appender.fileout.Append=false
    log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
    log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n
    

    Then put in the main class this block:

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

提交回复
热议问题