2

I would like to configure log4j to write only files up to a maximum specified size, e.g. 5MB. When the log file hits 5MB I want it to start writing to a new file. I'd like to put some kind of meaningful timestamp into the logfile name to distinguish one file from the next.

I do not need it to rename or manipulate the old files in any way when a new one is written (compression would be a boon, but is not a must).

I absolutely do not want it to start deleting old files after a certain period of time or number of rolled files.

Timestamped chunks of N MB logfiles seems like the absolute basic minimum simple strategy I can imagine. It's so basic that I almost refuse to believe that it's not provided out of the box, yet I have been incapable of figuring out how to do this!

In particular, I have tried all incarnations of DailyRollingFileAppender, RollingFileAppender or the 'Extras' RollingFileAppender. All of these delete old files when the backupcount is exceeded. I don't want this, I want all my log files!

TimeBasedRollingPolicy from Extras does not fit because it doesn't have a max file size option and you cannot associate a secondary SizeBasedTriggeringPolicy (as it already implements TriggeringPolicy).

Is there an out of the box solution - preferably one that's in maven central?

bacar
  • 9,761
  • 11
  • 55
  • 75
  • Have you considered calling `setMaxBackupIndex` with maxint as a parameter, effectively letting it roll on forever? – flup Oct 21 '13 at 18:57
  • 1
    @flup, that doesn't scale. With the regular `RollingFileAppender`, if you have N 'backup' files, each roll requires renaming all N files, which can be tremendously slow. The 'Extras' `RollingFileAppender` limits the backup count to 12 for this very reason. I really don't want or need it to rename any files if I can get the creation time (or some unique index - don't really care which) into the file name upon creation. – bacar Oct 21 '13 at 19:19
  • @flup actually it's even worse than that - if you have maxBackupIndex=N, it will attempt to find and rename all N files (even if they don't exist yet)... so maxint would definitely be out of the question! – bacar Oct 21 '13 at 19:28
  • 2
    Do you need to use log4j? Logback supports time and size based policy for rolling files that might work for you (http://logback.qos.ch/manual/appenders.html). – Mark Oct 21 '13 at 19:59
  • Interesting! I'd think the best solution then is to adapt the code for an existing appender. – flup Oct 21 '13 at 20:29
  • I've been resisting logback (partly because our organisation / support team are already familiar with log4j, partly because I haven't invested effort yet in figuring out if we can continue to use our custom appenders that we use for alerts) – bacar Oct 22 '13 at 07:34

1 Answers1

0

I gave up trying to get this to work out of the box. It turns out that uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender is, as the young folk apparently say, the bomb.

I tried getting in touch some time back to get the author to stick this into maven central, but no luck so far.

bacar
  • 9,761
  • 11
  • 55
  • 75