macOS Logging – Equivalent of logrotate on macOS

logginglogrotatemac-osxmac-osx-server

Is logrotate hiding somewhere on OSX, or is there an equivalent? It's not in /usr/sbin.

Best Answer

Based on Brian Armstrong's answer, here's something with a little more explanation and a correction. This handles the log created by postgres on OSX installed by Homebrew. Located at /etc/newsyslog.d/postgresql.conf:

# logfilename                           [owner:group]      mode count size(KB)  when  flags [/pid_file]                            [sig_num]
/usr/local/var/postgres/postgresql.log  :                  600  2     2048      *     J     /usr/local/var/postgres/postmaster.pid 

This will rotate the log file when it reaches 2MB in size, keep 2 archives (for a total of 6MB storage used), and bzip2-compress the archives. It will notify the postgres process to reopen the log files once rotated, which is necessary to get new log entries and to actually free the disk space without restarting the machine.

Important to note that size is in KB, not bytes.

You can test the config file (without affecting any files) using sudo newsyslog -nvv.

newsyslog documentation is located here: http://www.freebsd.org/cgi/man.cgi?newsyslog.conf(5). Also used: http://www.redelijkheid.com/blog/2011/3/28/adding-custom-logfile-to-os-x-server-log-rotation.html