using log4j for clearing a file?

血红的双手。 提交于 2019-12-02 04:28:22

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:

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.

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

log4j.appender.FA.append=false

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!