You are here

Log rotation on a Mac

When working with large web projects over a longer period of time on a local development machine. You may notice that your disk space (especially with a SSD) is running low and one of the reason could be your log files. So why not rotate them an automatically delete older ones.

On most Linux based servers you would use the logrotete script to handle log files, but not in OS X. It comes with newsyslog and its configuration can be found in /etc/newsyslog.conf and the files located in /etc/newsyslog.d/. I normally create a new, such as /etc/newsyslog.d/development.conf.

~$ sudo nano -w /etc/newsyslog.d/development.conf

Adding the following line(s).

# logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
/Users/cableman/source//sites//logs/*.log        644        5        5120        *        JG

As I don't want to go into this file every time I add a new site to my development server I use wildcards in the path as shown above. The first number set the permissions on the log files after rotation, namely read and write to the owner and read to any one else. The next two number tells newsyslog that I wants 5 rotations and that the file should first be rotate when its 5 Mb or lager. The asterisk simple tells newsyslog that I don't know or care about the PID file. The J tells it that I want to bzip2 rotated files and G that I'm using wildcards (*) in the path.

To verify that it works and simulate a rotation, you can run this command.

~$ newsyslog -nvv

If you want to perform the rotation right now simply run newsyslog without any parameters. OS X will automatically run the rotation together with all the other log files located on the system.


Mac OS X logs rotate apache


This is a great note, thank you. But perhaps it's worth clarifying that there are multiple tabs in the example line you show; copying and pasting it won't work. In particular there need to be double tabs for the skipped columns (eg, owner:group).

Not sure what was giving you trouble, but according to the man page fields are separated by white space, not specifically tab only delimiters. The owner:group field is optional and I believe is detected by the ':' in its syntax as that symbol is required even if you are only specifying just owner or just group. Flags can be empty (or just '-' to skip it), and /pid_file and signum are also optional. When I created my conf file, my editor automatically converts tabs to spaces and I didn't run into interpreter problems. Glad it sounds you got it working, regardless.

What does the 5120 represent?

Add new comment