How do I limit launchd log size on OS X?

I couldn't find a way to do this within the .plist file, however using newsyslog (rotate log files) instead seems to be the way forward.

Steps:

  • Edit /etc/newsyslog.conf
  • Add the following syntax:
<log filename>   [owner:group]  mode count size when flags [/pid_file] [sig_num]
  • Verify that the configuration file is correct with sudo newsyslog -nvv

So for example from the question, I'll add the following to /etc/newsyslog.conf:

/path/to/logfile.log               644  2     1000 *     J
/path/to/another_logfile.log       644  2     1000 *     J
  • mode: 644 (root can change it, everyone else can read it)
  • count: 2 (maximum number of archive files which may exist)
  • size: 1000 (max log file size of 1000 kilobytes)
  • when: * (log rotation solely depends on the size)
  • flags: J (newsyslog should attempt to save disk space by compressing the rotated log file using bzip)

See the newsyslog manual for more information.