digiarnie
digiarnie

Reputation: 23345

Appending current time to a new log file each time log4j is initialized

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.

Upvotes: 4

Views: 2278

Answers (2)

Pratyusha
Pratyusha

Reputation: 11

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>

Upvotes: 1

Catchwa
Catchwa

Reputation: 5855

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

Upvotes: 4

Related Questions