使用 log4j 清除文件?
我使用 log4j 写入具有以下属性文件的文件:
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
我的问题是,在每次运行程序时,我想清除文件“temp.ppr”,然后使用 lo4j 有效地写入它?或者您推荐其他解决方案?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在 < 上设置
append
属性code>FileAppender 改为false
?Have you tried just setting the
append
property on theFileAppender
tofalse
?您可以这样做:
然后在应用程序的启动代码中:
这样,对于程序的每次运行,现有日志都会移动到“temp.ppr.1”,并且“temp.ppr”会重新开始。这样,您也始终可以获得上次运行的日志。
You could do this:
And then in the startup code of the application:
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.
我建议作为构建工具(Apache Ant 或 Maven)的一部分来负责清除不需要的文件。或者,如果您想保留备份,可以使用 RollingFileAppender
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