问题
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