Introduction
I'd like to thank The Code Project members for allowing us to share our code on this site. This article is created to give online help about an appender class for the popular Log4j package, how to use it, its source code, and comments.
Background
Log4j is a very popular package for logging various kinds of messages from within a Java application. It's a feature rich logging utility and comes with various built-in appenders which allows one to redirect the logging output to various destinations like files, message queues, database, etc.
While working for one of the clients, I came across the requirement which demanded that the log files be prepared on a daily basis and be kept in a day-wise folder structure. Although the first requirement is easy to accommodate with the built-in DailyRollingFileAppender
class, the implementation for the second part was not readily available. So I decided to write my own appender to satisfy this need.
Using the Code
The download file contains the code and the sample log4j configuration file. Simply copy the DailyFolderAppender.class found under bin to your project's class folder. Keep in mind that the com.freeware.log4j directory structure is preserved. Copy the following lines from log4j.properties file in your project's log4j.properties file and start using this appender. The file is rolled-over at midnight.
## direct messages to a file date-wise ###
log4j.appender.Daily=com.freeware.log4j.DailyFolderAppender
log4j.appender.Daily.RootFolder=logs
log4j.appender.Daily.FileName=MyApp.log
log4j.appender.Daily.DatePattern=yyyy-MM-dd
log4j.appender.Daily.layout=org.apache.log4j.PatternLayout
log4j.appender.Daily.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
To use this configured appender, refer to the following snippet:
### set log levels - for more verbose logging change 'info' to 'debug' ##
### possible levels ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
log4j.rootLogger=INFO, stdout, Daily
History
- 18th June, 2004 - Initial release
I am a software professional with over 20 years of commercial business applications design and development experience.
My programming experience includes Java, Spring, .NET, Classic VB & ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.
From last 11 years I am mostly working with Java Technology. I am currently available to take up new assignments.