Search code examples
laravellaravel-5.6monolog

Using Monolog WebProcessor with Laravel 5.6


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.


Solution

  • 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.

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