4

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.

Tal C
  • 547
  • 1
  • 8
  • 17
  • See this: https://laracasts.com/discuss/channels/laravel/my-laravellog-size-is-31-gb?page=1&replyId=114005 – Hossein.Kiani Aug 10 '21 at 10:50
  • Thanks @Hossein.Kiani, I already do daily log rotation. The problem is if the daily logs are huge, is it possible to get a maximum file size? – Tal C Aug 10 '21 at 10:53
  • Did you try this config: log_max_files in the config/app.php file? – Hossein.Kiani Aug 10 '21 at 10:56
  • This will limit the amount of days back the log will show, not log size. – Tal C Aug 10 '21 at 11:08
  • try this https://www.milevis.com/tips/details/keep-limited-log-file-size-in-php-laravel – Rakesh kumar Oad Aug 10 '21 at 11:39
  • Maybe out of scope, but what are you logging? 1Gb sounds like a lot. If it are errors, please fix them. If it are trace logs, there are other ways. Log files are clumsy. Just a flat text file. My log files on a 40K+ page requests website generates a max of 1kb a day. – Dimitri Mostrey Aug 10 '21 at 14:21

3 Answers3

6

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
   }

zahra_oveyedzade
  • 988
  • 10
  • 17
0

It can be done by a custom log middleware which checks for file size and do changes accordingly.

I found this on internet, you can reference it.

https://gist.github.com/catzie/5511fb4ba0d0e386fd7d77209c9f004f

https://laravel.com/docs/8.x/middleware

kup
  • 731
  • 5
  • 18
-1

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 .

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24