Using Monolog WebProcessor with Laravel 5.6

房东的猫 提交于 2019-12-24 10:18:05

问题


I see that new logging stack/channels provides a way to tap or define handlers. However, I'm trying to get WebProcessor loaded and it doesn't seem to work. Should this be tapped? Or is there a different way to load this?

This is specific to Laravel 5.6. Here is what I used in my older application that uses Laravel 5.2 (bootstrap/app.php):

$app->configureMonologUsing(function (Monolog\Logger $monolog) {
    /* Include basic http props in logs */
    $webProcessor = new Monolog\Processor\WebProcessor();
    $monolog->pushProcessor($webProcessor);
});

@AkenRoberts I tried tap => Monolog\Processor\WebProcessor::class which I guess is not right.


回答1:


Ok. After a bit of research it seems like tap is the best way to hook processors. So, if I want to tag a processor to all the handlers in the current logging stack I could do be by add this:

tap => [[App\Logging\MyClass::class]]

This class in turn will push required processors onto all the handlers within it's __invoke method.

/**
 * Customize the given logger instance.
 *
 * @param  \Illuminate\Log\Logger  $logger
 * @return void
 */
public function __invoke($logger)
{
    foreach ($logger->getHandlers() as $handler) {
        $handler->pushProcessor(new WebProcessor);
    }
}


来源:https://stackoverflow.com/questions/51677615/using-monolog-webprocessor-with-laravel-5-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!