Laravel:使用`configureMonologUsing()`的多个日志提供程序?

问题描述 投票:4回答:3

我正在使用configureMonologUsing()添加两个自定义记录器。使用标准SOLID主体,我有两个提供程序:ConsoleLoggerProviderMailLogProvider

这两个都有类似的寄存器:

public function register()
{
    app()->configureMonologUsing(function(\Monolog\Logger $monolog) {
        $monolog->pushHandler(new HandlerClass());
    });
}

但是,我已经注意到记录器将覆盖另一个记录器...如何堆叠这些记录器?

我也尝试过使用boot(),但这没有用。我找不到其他方法可以添加到Monolog堆栈中。

优选,我也想堆叠到Laravel的内置记录器上。

php laravel laravel-5 monolog
3个回答
9
投票

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

在我的提供程序中,我没有使用configureMonologUsing(),而是使用了Log::getMonolog()->pushHandler([..])

行得通!所有记录器,包括内置的Laravel文件记录器,都在触发。最后!

(老实说,我一直在寻找一种方法来添加到Monolog堆栈中;显然,我没有按正确的条件进行搜索)


1
投票

根据Laravel documentation:

您应该在文件configureMonologUsing变量返回之前,立即在bootstrap/app.php文件中调用$app方法。

在这种情况下,应该对您有用:创建两个处理程序类,然后将它们添加到独白中(在bootstrap / app.php中:]

$app->configureMonologUsing(function ($monolog) {
  $monolog->pushHandler(new EmailLogHandler);
  $monolog->pushHandler(new ConsoleLogHandler); 
}); 
return $app;

0
投票

Laravel 5.2 docs之后,在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_to_local_diskservices.slack.send_errors_to_slack等可用。

http://stackoverflow.com/a/36259944/470749很有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.