Laravel Application Logs and Logrotate

Depending on usage, errors, and what you’ve chosen to log, the Laravel application log(s) can really grow after a while. With Laravel 3 and initially with Laravel 4, new log files were created every day by default, which kept them small and easy to search, but now Laravel 4 defaults to a single log file (which I prefer). Logrotate is a common utility on linux servers to handle the backup and rotation of server logs (Apache, syslogs, etc.) and it’s really simple to add additional logs/folders for Logrotate to handle. There’s a nice Digital Ocean tutorial for using Logrotate on Ubuntu 12.10, but here’s a quick and easy run through on how to use Logrotate with your Laravel logs:

 

In the shell, go to: /etc/logrotate.d In this folder are the configs that logrotate uses for different log locations (apache, mysql, syslog, apt, etc.). Create a new config: sudo touch mylaravelapp In the config file (use pico, nano, vim, whatever), add the location of the log files and the config settings:

/var/www/vhosts/[app_name]/app/storage/logs/*.log { 
    monthly 
    missingok 
    rotate 12 
    compress
    notifempty 
    create 755 www-data www-data 
}

The above code tells Logrotate to:

  • look for *.log files in laravels log directory.
  • create the backups monthly.
  • ignore and don’t throw an error if the file(s) are missing.
  • keep a year’s worth of backups. use compression (I believe gzip is the default).
  • Don’t rotate the log if it’s empty.
  • Create a replacement log file with the following permissions.

This last one is very important. If you create a new log file that the app cannot write to, the application will throw an error. You should also be able to use the nocreate option to just let Laravel create a new log file on it’s own the next time it needs to write to the log. To test this new config: sudo logrotate --force mylaravelapp And that should do it.

comments powered by Disqus