Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DailyRollingFileAppender

 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement for logging. Everyday a new log file has to be created with the current date (format YYYY-MM-DD)

Let say my current log file name is Mylog_2010-11-15.log. Next day a new log file has to be created with the date Mylog_2010-11-16.log

When i use DailyRollingFileAppender, it creates file name as "Mylog_2010-11-15.log." first time (when app started).

Next day what happens is "Current log FILLE renames TO Mylog_2010-11-15.log.10-11-15" and new file is still " Mylog_2010-11-15.log"

my requirement is very simple "instead of changing the extenstion with the old date (10-11-15)). Create new log file with 10-11-16.

Can anyone help on this "how to resolve this"

Regards,

Sankar. S

 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DailyRollingFileAppender is not designed to have the date in the "current" log's name. It is designed such that the current log is named something like MyLog.log, and then as it rolls, the archived logs are named Mylog_2010-11-15.log.

From the javadoc of DailyRollingFileAppender:

For example, if the File option is set to /foo/bar.log and the DatePattern set to '.'yyyy-MM-dd, on 2001-02-16 at midnight, the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log until it rolls over the next day.


As a side note, the RollingFileAppender in LogBack (a newer Logging Framework, written by the same people that first developed log4j, intended to be a successor to log4j) is more sophisticated and can be configured such that the "current" log name has the date in it. It can also be set to only keep x number of rolled logs (for example 60 days), deleting any older logs. This is a shortcoming of the DailyRollingFileAppender in Log4j as it cannot be configured to clean up old logs. You can use the Simple Logging Facade for Java (SLF4J) API to make it so LogBack handles all the Log4J logging for a project/application that uses log4j. It's as simple as replacing the log4j.jar file in your classpath with the log4j-over-sl4fj.jar from the SLF4J API and putting the logback jar in the classpath. Then replace your log4j configuration file with a logback configuration file. See the SLF4J Bridging legacy APIs page for details. Logback brings a number of advantages to the table over log4j. See the Reasons to prefer logback over log4j page for more information. (I use to be a log4j fanatic until I discovered SLF4J and Logback. Now that is the logging framework I evangelize, as you can probably tell.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic