1

How can I configure Log4j to generate new log file every time the java application runs? I want the log name to vary every time like [Log file name]_[CurrentTime].log I am using .property file to configure log4j

i am using RollingFileAppender but this did not worked for me.here is my configuration

log4j.appender.dest4=org.apache.log4j.RollingFileAppender
log4j.appender.dest4.File=.\\\\log4j\\\\user.log
log4j.appender.dest4.layout=org.apache.log4j.PatternLayout
log4j.appender.dest4.layout.ConversionPattern=[%d{dd/MMM/yyyy HH:mm:ss}] : %-5p: %m%n
log4j.appender.dest4.DatePattern='.'yyyy-MM
Jafar Ali
  • 1,084
  • 3
  • 16
  • 39

2 Answers2

0

You can set FileAppender dynamically

SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"logname."+new Date().toLocaleString(),false); logger.addAppender(appender);

SANN3
  • 9,459
  • 6
  • 61
  • 97
0

You might want to take a look at the RollingFileAppender

Here´s an interesting post about the subject which applies to log4j as well: How do I configure a RollingFileAppender to roll by date and size with log4net?

Hope it helps.

Community
  • 1
  • 1
Nano Taboada
  • 4,148
  • 11
  • 61
  • 89
  • If I am not wrong rolling file appended only rolls the log file to create new log file. All this time the application will be running. I want a new log file every time I run the application preferable appending date and time at the end of the file name. Its like keeping a session record each time the application runs. – Jafar Ali Jul 17 '12 at 13:14