log4j move rolling files to another directory/location

懵懂的女人 提交于 2019-12-19 03:39:14

问题


my goal is whenever file mylog.log is rolled to mylog.log.1 this rolled file is also moved to another directory, so in the original directory there is always only mylog.log. Is this possible using RollingFileAppender? Or another appender from default log4j library?


回答1:


I think you mean the RollingFileAppender from Log4J

if you use version >= 1.2.16 and the log4j-extras you can use this

log4j.rootCategory=INFO, base
log4j.appender.base=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.base.File=/tmp/logger.log
log4j.appender.base.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.base.rollingPolicy.maxIndex=5
log4j.appender.base.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.base.triggeringPolicy.maxFileSize=100
log4j.appender.base.rollingPolicy.ActiveFileName=/tmp/logger-%i.log
log4j.appender.base.rollingPolicy.FileNamePattern=/tmp/test/logger-%i.log.gz
log4j.appender.base.layout = org.apache.log4j.PatternLayout
log4j.appender.base.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

The important part is log4j.appender.base.rollingPolicy.FileNamePattern=/tmp/test/logger-%i.log.gz

But you have to make sure that the folder (in this example /tmp/test exists



来源:https://stackoverflow.com/questions/22866704/log4j-move-rolling-files-to-another-directory-location

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