简体   繁体   English

如何禁用 Laravel 5 日志文件?

[英]How to disable Laravel 5 log file?

My Laravel 5 website is working in a shared host with 1 GB disk space.我的Laravel 5网站在具有 1 GB 磁盘空间的共享主机中运行。 But now I have about 100 MB log file.但现在我有大约 100 MB 的日志文件。 How I can disable log file in Laravel 5 ?如何在Laravel 5禁用日志文件

In order to completely disable logging you'll need to overwrite the default log handlers that are set by Laravel.为了完全禁用日志记录,您需要覆盖 Laravel 设置的默认日志处理程序。 You can easily do this with你可以很容易地做到这一点

$nullLogger = new NullHandler();
\Log::getMonolog()->setHandlers(array($nullLogger));

You need to call as early as possible, before request is processed, eg you could do that in your bootstrap/app.php :您需要在处理请求之前尽早调用,例如,您可以在bootstrap/app.php 中执行此操作:

$app->configureMonologUsing(function($monolog) {
  $nullLogger = new \Monolog\Handler\NullHandler();
  $monolog->setHandlers(array($nullLogger));
});

return $app;

If your log is very large then either you are logging a lot or your application is throwing a lot of errors.如果您的日志非常大,那么要么您记录了很多日志,要么您的应用程序抛出了很多错误。 You should first examine the log and see if you can reduce data being written.您应该首先检查日志,看看是否可以减少写入的数据。

You can also switch to daily logging and then have a job to delete old log files.您还可以切换到每日日志记录,然后执行删除旧日志文件的作业。

The next thing to do would be to update your logging configuration to daily instead of the default single in config/app.php接下来要做的是将您的日志记录配置更新为每日而不是config/app.php中的默认单次

Laravel will handle rotating the file daily and deleting old log files after 5 days, or the value of the app.max_log_files if you need more kept. Laravel 将处理每日轮换文件并在 5 天后删除旧日志文件,或者如果您需要更多保留,则app.max_log_files的值。

I know that this a bit old, but:我知道这有点旧,但是:

In config/logging.php: In channels section add these:在 config/logging.php 中:在channels部分添加这些:

'none' => [
    'driver' => 'monolog',
    'handler' => \Monolog\Handler\NullHandler::class,
],

Then update your.env file to use this logger:然后更新 your.env 文件以使用此记录器:

LOG_CHANNEL=none

And everything should work just fine.一切都应该工作得很好。

Maximum Daily Log Files最大每日日志文件

When using the daily log mode, Laravel will only retain five days of log files by default.当使用每日日志模式时,Laravel 默认只会保留five days的日志文件。 If you want to adjust the number of retained files, you may add a log_max_files configuration value to your app configuration file:如果你想调整保留文件的数量,你可以在你的app配置文件中添加一个log_max_files配置值:

config >> app.php配置 >> app.php

'log_max_files' => 30

For more: https://laravel.com/docs/5.5/errors更多信息: https ://laravel.com/docs/5.5/errors

for me, the folder 'tmp' in wamp was growing up 15 giga,!对我来说,wamp 中的文件夹“tmp”增长了 15 giga! after some research I found out that the reason was the xdebug extension I simply adjut it not to send debug info, from php.ini :经过一些研究,我发现原因是 xdebug 扩展,我只是简单地调整它不发送来自php.ini的调试信息:

xdebug.profiler_enable = 0

You May disable the writing action Log:: is called by commenting the follow line:您可以通过注释以下行来禁用写入操作 Log:: 被调用:

Log::useFiles(storage_path().'/logs/laravel.log');

in start/global.phpstart/global.php

But first, you should find out why your log file is that big?但首先,您应该找出为什么您的日志文件那么大? Second, having some kind of archive function to your file will help as well.其次,为您的文件提供某种存档功能也会有所帮助。 Maybe twice a day.也许一天两次。

Hope it helps.希望能帮助到你。 Good Luck!祝你好运!

Yes definitively you need to edit core laravel file in order not to save log file..是的,您肯定需要编辑核心 laravel 文件才能不保存日志文件。

go to vendor->laravel->framework->src->Illuminate->Log->Writer.php and then comment out all the code within function __call like below.转到 vendor->laravel->framework->src->Illuminate->Log->Writer.php,然后注释掉函数 __call 中的所有代码,如下所示。

public function __call($method, $parameters)
{
    // if (in_array($method, $this->levels))
    // {
    //  call_user_func_array(array($this, 'fireLogEvent'), array_merge(array($method), $parameters));

    //  $method = 'add'.ucfirst($method);

    //  return $this->callMonolog($method, $parameters);
    // }

    // throw new \BadMethodCallException("Method [$method] does not exist.");
}

You log will never be save.您的日志将永远不会被保存。

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

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