Search code examples
javalog4jtext-filesappender

using log4j for clearing a file?


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.FileAppender
log4j.appender.FA.File=temp.ppr
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern= %m%n

My problem is , that in each run of my program , i want to clear the file "temp.ppr" , and then write to it efficiently with lo4j? or do you recommend other solutions?

thanks


Solution

  • 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.