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

Laravel: multiple log providers using `configureMonologUsing()`?

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

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

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

但是,我注意到 over logger 会覆盖另一个 logger...如何堆叠这些?

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

最好,我也想叠加到 Laravel 的内置记录器上。

根据 Laravel documentation:

You should place a call to the configureMonologUsing method in your bootstrap/app.php file right before the $app variable is returned by the file.

在那种情况下,thus 应该适合你:创建两个处理程序 类 并以这种方式将它们添加到独白中(在你的 bootstrap/app.php 中):

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

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

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

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

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

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 等可用)。

很有帮助。

这是我在 Laravel Lumen v5.4 上的配置方式 在 app.php:

$publisher = new \Gelf\Publisher(new \Gelf\Transport\HttpTransport(env('GRAYLOG_HOST'), env('GRAYLOG_PORT'), env('GRAYLOG_PATH')));

//WhatFailureGroupHandler does not break app execution 
//if some exceptions happen  happens while logging
$failureHandler = new \Monolog\Handler\WhatFailureGroupHandler([
 new \Monolog\Handler\GelfHandler($publisher)
]);

\Log::pushHandler($failureHandler);

\Log::getMonolog() 接受的答案引发错误。

还尝试使用 $app->configureMonologUsing() 配置,但抛出 A facade root has not been set. 错误。但最后,我发现那是因为我们需要 return logger:

$app->configureMonologUsing(function ($monolog) {

    $publisher = new \Gelf\Publisher(new \Gelf\Transport\HttpTransport(env('GRAYLOG_HOST'), env('GRAYLOG_PORT'), env('GRAYLOG_PATH')));
    
    $failureHandler = new \Monolog\Handler\WhatFailureGroupHandler([new \Monolog\Handler\GelfHandler($publisher)]);

    $monolog->pushHandler($failureHandler);

    //fixes error: A facade root has not been set
    return $monolog;
});

我看到的所有 $app->configureMonologUsing() 用法示例都没有 return 语句,即使在其他答案中,这对我不起作用。