2

In the current situation log files are rolled based on a maximum file size of 10MB. A filename of the rolled files is for instance "SystemOut_Debug.log.1".

The problem is the filename of these rolled log files. What we want is the current date and timestamp of the moment the file is rolled. For instance "SystemOut_Debug.20110505.104500.log"

How can we realize this?

Many thanks

Korenaga
  • 339
  • 2
  • 9
  • possible duplicate of [Setting a log file name to include current date in Log4j](http://stackoverflow.com/questions/192456/setting-a-log-file-name-to-include-current-date-in-log4j) – jmj May 05 '11 at 08:56

2 Answers2

3

Use a DailyRollingFileAppender instead, e.g.

<appender name="log" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="/srv/logs/myprogram.log" />
    <param name="Append" value="true" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
    </layout>
</appender>

The DatePattern can be used to rotate on hourly basis, e.g. use

<param name="DatePattern" value="'.'hh" />

to have one file per hour, overwritten every 24 hours.

rurouni
  • 2,315
  • 1
  • 19
  • 27
  • If you need to rotate exactly on 10 MB you can derive your own Appender class from DailyRollingFileAppender, but I believe in most situations it's not worth the effort. – rurouni May 05 '11 at 08:56
  • We had a talk with our client and he requests that the logfile is only rolled when it's 10MB and not because a certain amount of time is passed. Besides that file name must contain the entire date & timestamp. – Korenaga May 05 '11 at 09:17
  • What I understand is that the only possibility to realize this is to extend the RollingFileAppender class and implement this functionality? – Korenaga May 05 '11 at 09:20
  • I have not testet it, but maybe it is sufficient to override 'public String getFile()' in the RollingFileAppender to return a different file name each time. But as each Filename is different all files will last forever if you do not keep track of old files. Just remove them after some time using a cron job or scheduled task (same problem exists with DailyRolling and full dates). – rurouni May 05 '11 at 10:15
  • Sadly I don't have the time (at the moment) to analyse exactly what code changes must be performed in the extended class. Hopefully I can analyse this further soon. I'll come back on this. – Korenaga May 05 '11 at 11:46
0

DailyRollingFileAppender is what you exactly searching for.

<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>
L.Butz
  • 2,466
  • 25
  • 44
  • Source: http://stackoverflow.com/questions/192456/setting-a-log-file-name-to-include-current-date-in-log4j – L.Butz May 05 '11 at 08:55