如何禁用 Laravel 5 日志文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34652933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable Laravel 5 log file?
提问by Ashish Detroja
My Laravel 5website is working in a shared host with 1 GB disk space. But now I have about 100 MB log file. How I can disable log filein Laravel 5?
我的Laravel 5网站在具有 1 GB 磁盘空间的共享主机上运行。但现在我有大约 100 MB 的日志文件。如何在Laravel 5 中禁用日志文件?
采纳答案by jedrzej.kurylo
In order to completely disable logging you'll need to overwrite the default log handlers that are set by Laravel. You can easily do this with
为了完全禁用日志记录,您需要覆盖 Laravel 设置的默认日志处理程序。你可以很容易地做到这一点
$nullLogger = new NullHandler();
\Log::getMonolog()->setHandlers(array($nullLogger));
You need to call as early as possible, before request is processed, e.g. 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;
回答by devrider
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
如果您需要更多保留的值。
回答by Tymur Valiiev
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' => NullHandler::class,
],
Then update your .env file to use this logger:
然后更新您的 .env 文件以使用此记录器:
LOG_CHANNEL=none
And everything should work just fine.
一切都应该正常工作。
回答by Rashedul Islam Sagor
Maximum Daily Log Files
最大每日日志文件
When using the daily log mode, Laravel will only retain five days
of log files by default. If you want to adjust the number of retained files, you may add a log_max_files
configuration value to your app
configuration file:
使用每日日志模式时,Laravelfive days
默认只会保留日志文件。如果要调整保留文件的数量,可以log_max_files
在app
配置文件中添加配置值 :
config >> app.php
配置>> app.php
'log_max_files' => 30
For more : https://laravel.com/docs/5.5/errors
回答by Ryan Fung
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.php
在 start/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!
希望能帮助到你。祝你好运!
回答by Ye Lwin Soe
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.
您的日志将永远不会被保存。