Limit laravel log file size

Viewed 1980

I'm new to Laravel, we are using Laravel 5.8, and I have seen horror stories where the log is set to daily rotation, yet still reaches 1gb+ (I saw someone had their log reach over 400gb overnight). Is there a way to split log files up and/limit the amount of total log size that can be created so I don't use up my entire server storage and render it useless.

I have looked all over and didn't find anything that did this other than creating a cron job or something which I am not a fan of in this instance. Thanks in advance.

3 Answers

In Linux, there is a concept named logrotate, which can manage any log file based on date or size. I think in Laravel for managing log files based on size, the logrotate is an acceptable choice. for more info: Link1 Link2

sample:

  1. create a file for your application on: sudo touch /etc/logrotate.d/yourappname

  2. the content of yourappname file would be something like:

   

<your-project-directory-absolute-path>/storage/logs/*.log {
        size 10k
        missingok
        rotate 7
        compress
        notifempty
        create 0644  www-data  www-data
        su www-data www-data
   }

You try this

$filePath = storage_path() . '/logs/laravel.log'; 
$bakFilePath = storage_path() . '/logs/laravel.log.bak';
$maxFileSize = 11000000;
$shrinkedFileSize = 10000000;
 
$success = shrinkFile($filePath, $bakFilePath, $maxFileSize, $shrinkedFileSize);

also follow this url for more help

Ok, it's now possible this file is deleted automaticaly . You just need to update laravel, and add this in your config/app.php file:

'log_max_files' => 30

On Laravel You can find config/logging.php file:

'daily' => [
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    'days' => 30, // 0 for unilimitted logs
],

set the number of days logfile removed automaicaly .

Related