May 05, 2016

Logrotate for Forge

Laravel Forge (and Envoyer) keep your Laravel application's storage directory persistent through deployments. One side effect of this is that your storage/logs/laravel.log file is always growing.

Linux systems come with Logrotate, which can help us manage your growing log files. It's really simple - we can basically set it and forget it.

Note: This is best used when your logs are set to single rather than daily within your config/app.php file.

Logrotate for Forge

First, we need to know the application's log storage directory. If my application is foo.example.com, that location is likely /home/forge/foo.example.com/storage/logs.

Knowing that, we can make a logrotate configuration for it at /etc/logrotate.d/foo.example.com:

/home/forge/foo.example.com/storage/logs/*.log {
    su forge forge
    weekly
    missingok
    rotate 24
    compress
    notifempty
    create 755 forge forge
}

Once you save that file, you're done!

Let's go over the options we set there:

  • First we set the file path, and tell it to find any file ending in .log
  • su forge forge tells logrotate to use user/group forge to process these logs
  • weekly tells logrotate to rotate logs weekly (versus daily, monthly, yearly)
  • missingok - If no *.log files are found, don't freak out
  • rotate 24 - Keep 24 files before deleting old log archived (deletes oldest first). This is 24 weeks, or 6 months
  • compress - Compress archived log files so they take less disk space
  • notifempty - Don't rotate the log if it's empty
  • create 755 forge forge - Create new log files with these permissions/owner settings

Testing

To test this command out, we can force the running of logrotate with the following command:

# Forge logrotate to run against config file foo.example.com
sudo logrotate --force foo.example.com

This will tell you if there are any errors. If you get no output, you should be able to check your log directory and see some newly rotated logs!

Resources

All Topics