Failed to add date to file name using log4j DailyRollingFileAppender
Hey. I am trying to add the current date to the filename using log4j DailyRollingFileAppender, but it doesn't work. I used the configuration as shown below. Please suggest a solution for this
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 expect the log file to be app2014-11-07.log but its still app.log
source to share
If you are using log4j 1.x, we strongly recommend using 1 instead (may lose messages, Error 43374 ). org.apache.log4j.rolling.RollingFileAppender
org.apache.log4j.DailyRollingFileAppender
So your application config could 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
Notes
- In this case, you need to add the appropriate jar ( apache-log4j-extras-1.2.17.jar ).
source to share
DailyRollingFileAppender stands for archiving log files.
For example, today is 2014.11.07 when you first run your application, your log file name will be app.log
. Tomorrow you run the application again, the log file is also named app.log
, but the log file was last modified, perhaps asapp.log.2014.11.07
Try the following:
- Change your system date to 2014.11.08
- Run the application, then check the log path:
F:/temp/
OR
Change this
log4j.appender.rollingAppender.DatePattern='.'yyyy-MM-dd
to
log4j.appender.rollingAppender.DatePattern='.'yyyy-MM-dd-HH-mm
This means that it will produce a new log file every minute.
Start it up again.
source to share
You can use this config:
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
source to share