开发者

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 how开发者_JAVA技巧ever I want it to roll each and every time as opposed to just daily.


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


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜