using log4j for clearing a file?

后端 未结 3 1847
花落未央
花落未央 2020-12-22 01:49

im using log4j to write into a file with the following properties file :

log4j.rootLogger=DEBUG, FA

#File Appender
log4j.appender.FA=org.apache.log4j.FileAp         


        
相关标签:
3条回答
  • 2020-12-22 02:24

    You could do this:

    log4j.appender.FA=org.apache.log4j.RollingFileAppender
    log4j.appender.FA.MaxBackupIndex=1
    

    And then in the startup code of the application:

    ((RollingFileAppender)Logger.getRootLogger().getAppender("FA")).rollOver()
    

    That way, for each run of the program, the existing log is moved to "temp.ppr.1" and "temp.ppr" starts new. This way, you always have the log of the previous run as well.

    0 讨论(0)
  • 2020-12-22 02:24

    I would suggest as part of your build tool (Apache Ant or Maven) to take care of clearing the file you don't want. Alternatively if you want to keep a backup you can use the RollingFileAppender

    0 讨论(0)
  • 2020-12-22 02:31

    Have you tried just setting the append property on the FileAppender to false?

    log4j.appender.FA.append=false
    
    0 讨论(0)
提交回复
热议问题