How to set up Logrotate

When you are dealing with files that keep growing over time, log files for example, you will need a way of managing these so they won’t grow to an unmanageable size. A tool we use for that is logrotate. Since it’s already present on your Bolt, you can use that to manage files your site creates as well. Here is how you can set up logrotate on your site:

First Step: Creating the Configuration File

Since our default configuration only covers the log files generated by your site, we need to specify a new configuration file to handle our custom set-up as well. It’s highly recommended to place this file outside of the public folder, for example in the /private/ folder instead. 

Creating the configuration file can be done in a myriad of ways. In this how-to, we’ll be making a few assumptions on how our logrotate needs to run. You can, of course, adjust these settings, or even amend them using the online logrotate documentation. For our purposes, we will use the following settings: we instruct logrotate to run on all files with the .log extension found in the given path, log files are rotated daily, it’s ok if a file is missing, we keep a maximum of four file rotations, rotated logs are compressed, upon rotation a new log file will be created and when the log is rotated the date will be appended to the filename. Combining that would give us the following as a configuration file:

/kunder/<boltID>/<environmentID>/private/*.log {
daily
missingok
rotate 4
compress
create
dateext
}

I’ve used placeholders named boltID and environmentID here. These should match up with the actual file path on your environment. These can be obtained through our Admin Panel, or you can use the pwd command on the command line to quickly see what they should be. You can save this file under any name you prefer. In this case, I chose to save it as a file named rot.conf. It’s quickest to use SSH to place this file on your environment, since that is needed for the following step as well.

Second Step: Test if our Logrotate Configuration Works

Running logrotate will require a state file to use as well. Since the default state file cannot be used we need to specify our own. It doesn’t have to exist just yet, as it will be created if it’s not found. Besides that, we will use the –verbose flag while testing to see some more information on what is happening.

Assuming we are in the directory where our configuration file is present as well, we can test the configuration using the following command. What follows is an example of the output it produces. It will tell you here if something is going wrong that needs to be fixed.

logrotate --state logrotate.state rot.conf --verbose

After issuing this command, you will get output in the terminal that resembles this:

reading config file rot.conf
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /kunder/<boltID>/<environmentID>/private/*.log daily (4 rotations)
empty log files are rotated, old logs are removed
considering log /kunder/<boltID>/<environmentID>/private/debug.log
log needs rotating
rotating log /kunder/<boltID>/<environmentID>/private/debug.log, log->rotateCount is 4
dateext suffix '-2024022202'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /kunder/<boltID>/<environmentID>/private/debug.log to /kunder/<boltID>/<environmentID>/private/debug.log-2024022202
creating new /kunder/<boltID>/<environmentID>/private/debug.log mode = 0644 uid = 22925 gid = 14693
compressing log with: /bin/gzip

After a few rounds of testing, you will end up with files looking like this:

Directory listing showing files that have been handled by logrotate.

Final Step: Setting up Cron so Logrotate runs Automatically

We want this to run daily, and preferably in a way that doesn’t require our interaction. For that, we can set up a cron job in the Admin Panel. 

For timing, we enter the following:

14 1 * * *

This will ensure that this cron job gets executed every day, at 01:14. 

For the command, we enter the following:

/usr/sbin/logrotate /kunder/<boltID>/<environmentID>/private/rot.conf --state /kunder/<boltID>/<environmentID>/private/logrotate.state

Don’t forget to update the boltID and environmentID placeholders to match your scenario, or you will be getting emails about failing cron jobs.

After this is done, your cron job will be run daily, rotating the log files specified in the configuration you’ve provided.