3

How I can create a uniquely named log file using log4j?. When I used Logback, I can do it like this:

 <!-- current time formatted as "yyyyMMdd'T'HHmmss".  This value will be available to all 
    subsequent configuration elements. -->
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" />

and inside logger appender define file name pattern:

<file>${bySecond}.log</file>
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286

2 Answers2

0

I believe there's no support for this feature in log4j 1.7 out of the box. But you can extend RollingFileAppender's setFile method by altering fileName parameter to support the time-based feature.

mindas
  • 26,463
  • 15
  • 97
  • 154
0

the DailyRollingFileAppender appends the current date afer rolling over to the next file, so you get a unique name ending with the date of the log for every file. Cite JavaDoc:

For example, if the File option is set to /foo/bar.log and the DatePattern set to '.'yyyy-MM-dd, on 2001-02-16 at midnight, the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log until it rolls over the next day.

add something like this to your properties XML file:

<appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="application.log" />
    <param name="DatePattern" value=".yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" 
          value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n  %-5p %m%n"/>
    </layout>
  </appender>
Community
  • 1
  • 1
Simulant
  • 19,190
  • 8
  • 63
  • 98