Rotating logs generated by a process that logs to stdin

Solution 1:

Multilog from DJB's daemontools can do (almost) exactly what you are asking. The only downside that I am aware of is that not many distributions carry a package for daemontools.

If you are not managing your application with svc (part of the daemontools) you will have to find a way to pipe the output to a command like

multilog t s1048576 n100 ./my_log_directory

This translates to:

  • t: insert a tai64n timestamp (that can be translated to readable time with tai64nlocal)
  • s1048576: rotate the log file when it grows to 1MiB
  • n100: keep no more than 100 rotated files
  • ./my_log_directory: anything starting with . or / - write the log to that directory

The log that is written will have the filename current, and when multilog rotates the log, it will be renamed to @<tai64n timestamp>.s where the filename shows the time when the file was rotated. The extension can be .s if the file was safely flushed, or .u if it may have been truncated.

For more info, just check the links.