How to rotate logs in laravel using logrotate? [duplicate]
Asked Answered
T

1

5

Deployed my laravel application few months ago, and recently found out that my application was unable to write to log because my disk was full. When i checked i found out that the laravel.log file was almost huge. I want to know how to rotate the laravel logs with logrotate.

Tabu answered 9/2, 2019 at 21:23 Comment(4)
How does your current logging configuration (config/logging.php) look like?Perren
Possible duplicate of How to configure logrotate with php logs. There are so many answers regarding logrotate already. Please use the search before asking a question.Kristopherkristos
The file was almost huge, XD.Lindholm
@Mike Doe stop nagging and answer some questions - this question is straight to the point and first in a search.Jaunita
A
16

Go to /etc/logrotate.d and create a new config file.

cd /etc/logrotate.d
sudo touch laravel_rotate

Add the log file location and some settings.

log/file/dir/laravel_log.log { 
    monthly 
    missingok 
    rotate 12 
    compress
    notifempty 
    create 755 www-data www-data 
}

Settings explained:

  1. monthly: backups will be created monthly
  2. missingok: ignore the file if it is missing
  3. rotate 12: keep a year’s worth of backups
  4. notifempty: do not rotate the log if it’s empty
  5. compress: compress the log file
  6. create: create a replacement log file with the following permissions

To test this new config, run sudo logrotate --force laravel_rotate

logrotate doc

Auteur answered 9/2, 2019 at 21:38 Comment(2)
Great, also it is possible to rotate log files based on size.Driver
@Auteur is rotate better described as the number of backups to keep, so rotate 10 would give 10 weeks of backups if weekly (default) was setJaunita

© 2022 - 2024 — McMap. All rights reserved.