无法使用log4j DailyRollingFileAppender将日期附加到文件名 [英] Unable to append the date to the file name using log4j DailyRollingFileAppender
问题描述
您好我正在尝试使用log4j DailyRollingFileAppender将当前日期附加到文件名,但它不起作用。我使用了如下配置。请为此建议解决方案
Hi I am trying to append the current date to the file name using log4j DailyRollingFileAppender, but its not working. I have used the configuration like below. Please suggest a solution for this
log4j.rootLogger = DEBUG, rollingAppender
log4j.appender.rollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.rollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.rollingAppender.File=F:/temp/app.log
log4j.appender.rollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
我希望日志文件为app2014-11-07。 log,但它仍然是app.log
I am expecting the log file as app2014-11-07.log, but its still app.log
推荐答案
如果您使用的是log4j 1.x,我们强烈建议您使用< a href =https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html\"rel =nofollow> org.apache .log4j.rolling.RollingFileAppender
1 而不是 org.apache.log4j.DailyRollingFileAppender
(可能会丢失消息,错误43374 )。
If you're using log4j 1.x, we strongly recommend that you use org.apache.log4j.rolling.RollingFileAppender
1 instead of org.apache.log4j.DailyRollingFileAppender
(may lose messages, Bug 43374).
所以你的appender的配置可以是:
So the configuration of you appender can be:
log4j.rootLogger = DEBUG, rollingAppender
log4j.appender.rollingAppender=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.rollingAppender.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.rollingAppender.rollingPolicy.fileNamePattern=F:/temp/app%d{yyyy-MM-dd}.log
log4j.appender.rollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
注释
- 在这种情况下,您需要添加相应的jar(< a href =http ://search.maven.org/remotecontent?filepath = log4j / apache-log4j-extras / 1.2.17 / apache-log4j-extras-1.2.17.jarrel =nofollow> apache-log4j-extras-1.2 .17.jar )。
- In that case, you need to add the respective jar (apache-log4j-extras-1.2.17.jar).
这篇关于无法使用log4j DailyRollingFileAppender将日期附加到文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!