java 无法使用 log4j DailyRollingFileAppender 将日期附加到文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26802822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to append the date to the file name using log4j DailyRollingFileAppender
提问by phani sekhar
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 DailyRollingFileAppender 将当前日期附加到文件名,但它不起作用。我使用了如下配置。请为此提出解决方案
properties
特性
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
I am expecting the log file as app2014-11-07.log, but its still app.log
我期待日志文件为 app2014-11-07.log,但它仍然是 app.log
采纳答案by Paul Vargas
If you're using log4j 1.x, we strongly recommend that you use org.apache.log4j.rolling.RollingFileAppender
1instead of org.apache.log4j.DailyRollingFileAppender
(may lose messages, Bug 43374).
如果您使用的是 log4j 1.x,我们强烈建议您使用1而不是(可能会丢失消息,Bug 43374)。org.apache.log4j.rolling.RollingFileAppender
org.apache.log4j.DailyRollingFileAppender
So the configuration of you appender can be:
所以你 appender 的配置可以是:
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
Notes
笔记
- In that case, you need to add the respective jar (apache-log4j-extras-1.2.17.jar).
- 在这种情况下,您需要添加相应的 jar ( apache-log4j-extras-1.2.17.jar)。
回答by youngzy
DailyRollingFileAppendermeans archiving log files.
For example, today is 2014.11.07, when you first run your app, your log file name would be app.log
. Tomorrow, you run the app again,it's log file also named app.log
, butyesterday's log file has been changed , maybe like app.log.2014.11.07
DailyRollingFileAppender意味着归档日志文件。
例如,今天是 2014.11.07,当您第一次运行您的应用程序时,您的日志文件名将是app.log
. 明天,你再次运行该应用程序,它的日志文件也命名为app.log
,但昨天的日志文件已更改,可能像app.log.2014.11.07
Try this :
试试这个 :
- Change your System Date, to be 2014.11.08
- Run your app, then check out the log-path:
F:/temp/
- 将您的系统日期更改为 2014.11.08
- 运行您的应用程序,然后查看日志路径:
F:/temp/
OR
或者
Change this
改变这个
log4j.appender.rollingAppender.DatePattern='.'yyyy-MM-dd
to
到
log4j.appender.rollingAppender.DatePattern='.'yyyy-MM-dd-HH-mm
That means it will produce a new log file minute-by-minute.
Run it again.
这意味着它将每分钟生成一个新的日志文件。
再次运行它。
回答by Chetan chadha
You can use this configuration:
您可以使用此配置:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.stdout.layout=org.apache.log4j.HTMLLayout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%d](%F:%L) - %m%n
#log4j.appender.stdout.File=/usr/tomcat7/webapps/ngp/newgen.log
log4j.appender.stdout.File=c:/logs/DSP.log
#log4j.appender.stdout.DatePattern='.'yyyy-MM-dd
log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.MaxBackupIndex=1
log4j.appender.stdout.append=false