How to rotate the Laravel logs

Laravel stores the logs of the project in the file “storage/logs/laravel.log”. If you don’t rotate this file can increase its size a lot, using the most part of the storage, causing problems with a full disk.

To avoid these problems you can add this project to the logrotate of your server. You have to add a new file in the “/etc/logrotate.d/” folder:

$ sudo touch /etc/logrotate.d/my-proyect

Then you have to add the config in this file:

/var/my-project/storage/logs/laravel.log {
    weekly
    missingok
    rotate 12
    compress
    notifempty
    su my-user my-group
    create 700 my-user my-group
}

Now I explain the parameters:

  • /var/my-project/storage/logs/laravel.log is the file you want to rotate.
  • weekly will rotate the file each week.
  • missingok will ignore the file if it is missing.
  • rotate 12 keep the logs of the last 12 weeks.
  • notifempty does not rotate the log if it is empty.
  • compress will compress the rotated old log files.
  • su my-user my-group will rotate log files set under this user and group instead of using default user/group (usually root).
  • create 700 my-user my-group will create a new log file with the following permissions.

To check this config file you can execute the next command (in debug mode):

$ logrotate -d /etc/logrotate.d/my-proyect

You can find more information with the man command:

$ man logrotate

and in those links:

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.