Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am new to using apache logger . I have downloaded the log4j-xx and i have the following text configuration file

# Set root logger level to DEBUG and its only appender to mainFormat.
log4j.rootLogger = TRACE, mainFormat, FILE


# mainFormat is set to be a ConsoleAppender.
log4j.appender.mainFormat=org.apache.log4j.ConsoleAppender

# mainFormat uses PatternLayout.
log4j.appender.mainFormat.layout=org.apache.log4j.PatternLayout
log4j.appender.mainFormat.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

#File makes a file of the output.
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=log4j_HAPR001_OutputFile.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

i use the above config file to create the log file. Now i wanted to add the current time stamp to the log file. Is there any way to do this. If yes can some one please give me the instructions how to do. Thanks in advance.

Regards, Swati

share|improve this question
add comment

migrated from stackoverflow.com May 28 '10 at 4:43

This question came from our site for professional and enthusiast programmers.

2 Answers

The DailyRollingFileAppender of Log4j does exactly what you want.

share|improve this answer
    
Don't forget to use the correct fileformat to name the file. –  adaptr Dec 21 '11 at 8:56
add comment

Download yourself a copy of org.apache.log4j.FileAppender (example).

Copy it into your own namespace and refactor...

Comment out the 'getName' and 'setName' properties and add 'getDirectory' and 'setDirectory' properties.

The clever stuff comes when it makes the activateOptions() call and you can now dynamically create the fileName variable on the fly. In your case you'll need to use the Calendar class.

Don't forget to change the log4j.properties file, for example:

#File makes a file of the output.
#log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE=com.yourcompany.yourFileAppender
#log4j.appender.FILE.File=log4j_HAPR001_OutputFile.log
log4j.appender.FILE.Directory=${catalina.base}/logs
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.