Laravel 开发和生产的 5 种不同日志级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32168163/
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
Laravel 5 different log levels for development and production
提问by dev7
I'm using Laravel 5.1 and trying to set different logging logic for a development and production environment.
我正在使用 Laravel 5.1 并尝试为开发和生产环境设置不同的日志记录逻辑。
Throughout my application I am using the Log
facade with most of the following different methods:
在我的整个应用程序中,我使用Log
了以下大多数不同方法的外观:
Log::emergency($error);
Log::alert($error);
Log::critical($error);
Log::error($error);
Log::warning($error);
Log::notice($error);
Log::info($error);
Log::debug($error);
However, in my production environment, I would like to only log anything that is an Error
, Critical
, Alert
or Emergency
priority and ignore log requests with lower priority.
然而,在我的生产环境,我想只记录任何是Error
,Critical
,Alert
或Emergency
优先而忽略低优先级的日志请求。
I couldn't find anything in the documentation or by exploring the code (both Log
facade and the Monolog
class).
我无法在文档中或通过探索代码(Log
外观和Monolog
类)找到任何内容。
My current thought is to create a custom wrapper around the Log facade that simply checks the environment and ignores anything below 400 (Monolog level for Error). Basically I would create a threshold variable in the environment file and anything below it will simply not be logged to the files.
我目前的想法是围绕 Log 外观创建一个自定义包装器,它只是检查环境并忽略低于 400(错误的 Monolog 级别)的任何内容。基本上我会在环境文件中创建一个阈值变量,它下面的任何内容都不会被记录到文件中。
Before I do so, I wanted to ask the community if there is an existing method/configuration for that which I could use, so that I don't re-invent the wheel.
在我这样做之前,我想问社区是否有我可以使用的现有方法/配置,这样我就不会重新发明轮子。
If not - what would be the best approach?
如果没有 - 最好的方法是什么?
采纳答案by shock_gone_wild
This gistshows a more comfortable answer, as is not dependent on the chosen handler.
这个要点显示了一个更舒适的答案,因为它不依赖于选择的处理程序。
I'm just providing the essential part in an answer here in case the above link gets deleted in some time.
我只是在这里提供答案中的重要部分,以防上述链接在一段时间内被删除。
In the AppServiceProviders' register method:
在 AppServiceProviders 的注册方法中:
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
$monolog = Log::getMonolog();
foreach($monolog->getHandlers() as $handler) {
$handler->setLevel(Config::get('app.log-level'));
}
}
Then just add an additional key to your config/app.php:
然后只需在您的 config/app.php 中添加一个额外的键:
'log-level' => 'info', // or whatever minimum log level you would like.
回答by jedrzej.kurylo
Add the following code to your AppServiceProvider::register():
将以下代码添加到您的AppServiceProvider::register():
$this->app->configureMonologUsing(function ($monolog) {
$monolog->pushHandler(
$handler = new RotatingFileHandler(
$this->app->storagePath() . '/logs/laravel.log',
$this->app->make('config')->get('app.log_max_files', 5),
$this->app->make('config')->get('app.level', 'debug')
)
);
$handler->setFormatter(new LineFormatter(null, null, true, true));
});
This recreates the logic that Laraveldoes when setting up the dailyhandler, but adds passing level to the handler.
这重新创建了Laravel在设置日常处理程序时所做的逻辑,但为处理程序增加了传递级别。
You can set your minimum logging level by setting levelvalue in your config/app.php:
您可以通过在config/app.php 中设置级别值来设置最低日志记录级别:
'level' => 'debug', //debug, info, notice, warning, error, critical, alert, emergency
This is a bit of a workaround and each type of handler would need to be set up separately. I'm currently working on a pull-request to Laravelthat would add setting minimum debug level from the config file without writing a line of code in your AppServiceProvider.
这是一种解决方法,每种类型的处理程序都需要单独设置。我目前正在对Laravel进行拉取请求,该请求将从配置文件中添加设置最低调试级别,而无需在AppServiceProvider 中编写一行代码。
The code above hasn't been tested, so let me know if you see any typos or something doesn't work properly and I'll be more than happy to make that work for you.
上面的代码尚未经过测试,因此如果您发现任何拼写错误或某些内容无法正常工作,请告诉我,我将非常乐意为您服务。