4

Every single time I run my app, I want a new log file to be generated with the time stamp. Something like MyFile-4Nov2010-132122.log.

I've seen the use of the DailyRollingFileAppender however I want it to roll each and every time as opposed to just daily.

Community
  • 1
  • 1
digiarnie
  • 22,305
  • 31
  • 78
  • 126

2 Answers2

4

Subclass FileAppender or DailyRollingFileAppender to create a new file when the appender is instantiated.

Catchwa
  • 5,845
  • 4
  • 31
  • 57
  • Thanks Catchwa. Actually easier than I thought to create my own with a custom file name by overriding "void setFile(String);" – digiarnie Nov 04 '10 at 03:37
1

You can also configure the XML config file as below:

<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
  <param name="File" value="./logs/message"/>
  <param name="Append" value="true"/>
  <!-- Rollover at midnight each minute -->

  <param name="DatePattern" value="'-'yyyy-MM-dd'.log'"/>
  <layout class="org.apache.log4j.PatternLayout">
      <!-- The default pattern: Date Priority [Category] Message\n 
      <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>-->
      <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n -->
      <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>  
  </layout>

javanna
  • 59,145
  • 14
  • 144
  • 125
Pratyusha
  • 11
  • 2