Php Larvel:多个日志提供程序使用`ConfigureMonogusing()`?

Php Larvel:多个日志提供程序使用`ConfigureMonogusing()`?,php,laravel,laravel-5,monolog,Php,Laravel,Laravel 5,Monolog,我正在使用configuremonogusing()添加两个自定义记录器。使用标准的SOLID原则,我有两个提供程序:ConsoleLogProvider和MailLogProvider 这两个机构的登记册类似于: 公共函数寄存器() { app()->configuremonogusing(函数(\monog\Logger$monog){ $monolog->pushHandler(新HandlerClass()); }); } 然而,我已经注意到过记录器将覆盖另一个记录器。。。我如何堆叠这

我正在使用
configuremonogusing()
添加两个自定义记录器。使用标准的SOLID原则,我有两个提供程序:
ConsoleLogProvider
MailLogProvider

这两个机构的登记册类似于:

公共函数寄存器()
{
app()->configuremonogusing(函数(\monog\Logger$monog){
$monolog->pushHandler(新HandlerClass());
});
}
然而,我已经注意到过记录器将覆盖另一个记录器。。。我如何堆叠这些

我也尝试过使用
boot()
,但没有成功。我找不到任何其他方法来添加到独白堆栈中

更可取的是,我还想叠加到Laravel的内置记录器上。

根据

您应该在
bootstrap/app.php
文件返回
$app
变量之前,调用
configuremonogusing
方法

在这种情况下,您应该可以这样做:创建两个处理程序类,并以这种方式将它们添加到monolog中(在bootstrap/app.php中):

我(终于)找到了我的问题的答案:

在我的提供程序中,我使用的不是
configuremonogusing()
,而是
Log::getmonog()->pushHandler([…])

真管用!所有记录器,包括内置的Laravel文件记录器,都在开火。终于

(老实说,我已经寻找了好几天的方法来添加到Monolog堆栈中;我显然没有按正确的术语进行搜索)

接下来,在
bootstrap/app.php
中,我在
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\u to\u local\u disk
services.slack.send\u errors\u to\u slack
等可用)


有帮助。

这种方法的唯一问题是它也会覆盖Laravel内置的记录器。我刚刚找到了另一种方法:使用Log::getMonolog()代替app(),并使用configure using。这很有效,而且保留了拉威尔的记录器。哦,是的。完全忘记了这一点。请注意不要链接到“主”文档,而是链接到版本化文档:以下是使用文档中描述的方法的答案:
$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);
    }
});