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

自闭症网瘾萝莉.ら 提交于 2019-12-07 08:44:43

问题


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.


回答1:


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




回答2:


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>



来源:https://stackoverflow.com/questions/4093382/appending-current-time-to-a-new-log-file-each-time-log4j-is-initialized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!