Click here to Skip to main content
Licence CPOL
First Posted 17 Jun 2004
Views 30,665
Downloads 450
Bookmarked 10 times

Daily Folder Appender

By | 17 Jun 2004 | Article
A Log4j appender class to create daily folders

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prasad Khandekar

Architect
Fundtech INDIA Ltd.
India India

Member

I am a software professional with over 16 years of commercial business applications design and development experience.
 
My programming experience includes Java, .NET, Classic VB, ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.
 
For last 5 years I am mostly working on Java platform.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 18 Jun 2004
Article Copyright 2004 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid