'Limit laravel log file size
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.
Solution 1:[1]
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
Solution 2:[2]
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:
create a file for your application on:
sudo touch /etc/logrotate.d/yourappname
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
}
Solution 3:[3]
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 .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kup |
Solution 2 | |
Solution 3 |