简体   繁体   English

"限制 laravel 日志文件大小"

[英]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).我是 Laravel 的新手,我们使用的是 Laravel 5.8,我看到了日志设置为每日轮换的恐怖故事,但仍然达到 1gb+(我看到有人的日志在一夜之间达到了 400gb 以上)。 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.除了创建一个 cron 作业或在这种情况下我不喜欢的东西之外,我已经查看了所有内容,没有找到任何这样做的东西。 Thanks in advance.提前致谢。

"

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://gist.github.com/catzie/5511fb4ba0d0e386fd7d77209c9f004f

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

In Linux, there is a concept named logrotate<\/code> , which can manage any log file based on date or size.在 Linux 中,有一个名为logrotate<\/code>的概念,它可以根据日期或大小管理任何日志文件。 I think in Laravel for managing log files based on size, the logrotate<\/code> is an acceptable choice.我认为在 Laravel 中根据大小管理日志文件, logrotate<\/code>是一个可以接受的选择。 for more info: Link1<\/a> Link2<\/a>欲了解更多信息: Link1<\/a> Link2<\/a>

"

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM