Click here to Skip to main content
16,020,315 members
Articles / Programming Languages / Java / Java SE
Article

Daily Folder Appender

Rate me:
Please Sign up or sign in to vote.
4.54/5 (7 votes)
17 Jun 2004CPOL1 min read 52.8K   751   12   3
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)


Written By
Software Developer (Senior) Freelancer
India India
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.

Comments and Discussions

 
QuestionDailyFileAppender Issue Pin
Member 1123859812-Feb-15 4:58
Member 1123859812-Feb-15 4:58 
AnswerRe: DailyFileAppender Issue Pin
Prasad Khandekar19-Mar-15 17:37
professionalPrasad Khandekar19-Mar-15 17:37 
Hello,

Please have a look at the code for method subAppend in DailyFolderAppender.java. Basically you need to perform a rollover when date changes. It's not automatic. The logic is to check the current date with previous & on change the new folder and start appending to file in that folder.

Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.

GeneralMy vote of 5 Pin
AbhijitT.Net7-Apr-13 0:26
professionalAbhijitT.Net7-Apr-13 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.