我使用configureMonologUsing()
添加两个自定义记录器。做标准的SOLID主体,我有两个提供者:ConsoleLoggerProvider
和MailLogProvider
。
这两个都有类似的注册表:
public function register()
{
app()->configureMonologUsing(function(\Monolog\Logger $monolog) {
$monolog->pushHandler(new HandlerClass());
});
}
但是,我注意到记录器会覆盖另一个记录器......如何堆叠这些?
我也试图使用boot()
,但这并没有奏效。我无法找到任何其他方式添加到Monolog堆栈。
最好,我也想叠加到Laravel的内置记录器上。
答案 0 :(得分:9)
我(终于)找到了答案我的问题:
在我的提供商中,我使用configureMonologUsing()
Log::getMonolog()->pushHandler([..])
有效!所有记录器,包括内置的Laravel文件记录器,都在激活。最后!
(我老老实实地一直在寻找添加到Monolog堆栈上的方法;我显然没有用正确的术语进行搜索)
答案 1 :(得分:1)
您应该在文件返回
configureMonologUsing
变量之前调用bootstrap/app.php
文件中的$app
方法。
在这种情况下,因此应该适合你:创建两个处理程序类并以这种方式将它们添加到monolog(在你的bootstrap / app.php中):
$app->configureMonologUsing(function ($monolog) {
$monolog->pushHandler(new EmailLogHandler);
$monolog->pushHandler(new ConsoleLogHandler);
});
return $app;
答案 2 :(得分:0)
在bootstrap/app.php
的{{3}}之后,我在return $app;
之前添加了以下代码:
$app->configureMonologUsing(function($monolog) {//IMPORTANT: I think the order of pushHandler matters, and the ones defined last here will be the first to be called, which affects anything where bubble=false
if (config('services.slack.send_errors_to_slack')) {
$bubble = false; //I think that if I set the 'bubble' argument to false and handle the most severe logging levels first (which counterintuitively means lower in this function), less severe logging levels don't bother reporting the same message.
$useShortAttachment = false;
$includeContextAndExtra = true; //This is important because otherwise 404 errors wouldn't report the URL, give how 'report' function is coded within App\Exceptions\Handler.php.
$handlerForWarningsToNotifyPhone = new \Monolog\Handler\SlackHandler(config('services.slack.token'), config('services.slack.channel_warnings'), 'Monolog', true, null, \Monolog\Logger::WARNING, $bubble, $useShortAttachment, $includeContextAndExtra);
$monolog->pushHandler($handlerForWarningsToNotifyPhone);
$handlerForErrorsToNotifyPhone = new \Monolog\Handler\SlackHandler(config('services.slack.token'), config('services.slack.channel_errors'), 'Monolog', true, null, \Monolog\Logger::ERROR, $bubble, $useShortAttachment, $includeContextAndExtra);
$monolog->pushHandler($handlerForErrorsToNotifyPhone);
}
if (config('app.send_logs_to_loggy')) {
$logglyHandler = new \Monolog\Handler\LogglyHandler(config('services.loggly.token'), config('app.send_logs_to_loggy')); //See \Monolog\Logger::INFO. Log level 200 is "info".
$logglyHandler->setTag(config('services.loggly.tag'));
$monolog->pushHandler($logglyHandler);
}
if (config('app.log_to_local_disk')) {
$localHandler = new \Monolog\Handler\StreamHandler(storage_path("/logs/laravel.log"));
$monolog->pushHandler($localHandler);
}
});
这只是一个可以帮助您的示例。
请确保相应地编辑配置文件(例如,使app.log_to_local_disk
,services.slack.send_errors_to_slack
等可用)。
Laravel 5.2 docs很有帮助。