lundi 23 mars 2020

Laravel MonoLog, pushProcessor not Logging Addition attributes

I want to be able to add a unique id (Uid) to my logging.

In Example 1: Which is depended on config/logging.php and ProcessorTap files below is not working as expected. The logging is configured to use stdout which refers to the ProcessorTap class that is suppose to add a Uid, when the log statement is created (in accordance with UidProcessor)

Example 2: Which uses purely Mono classes works as expected.

Why isnt Example 1 adding the Uid to the logs, when laravel ("laravel/framework": "5.7.*") should be using Monolog classes as well ?

Example 1: When this api is invoked, the output for Log::info('test') does not include UiD

Route::get('/test', function () {
   Log::info('test'); //output = [2020-03-24 04:51:16] local.INFO: test  
});

config/logging.php:

   'default' => env('LOG_CHANNEL', 'stdout'),   //.env LOG_CHANNEL=stdout    
    'stdout' => [
                 'driver' => 'monolog',
                 'handler' => StreamHandler::class,
                 'with' => [
                     'stream' => 'php://stdout',
                 ],
                'tap' => [
                   ProcessorTap::class,
                ],
             ]      

ProcessorTap:

use Monolog\Processor\UidProcessor;

class ProcessorTap
{
    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($logger)
    {
        $logger->pushProcessor(new UidProcessor());
    }
}

Example 2: Working correctly the Uid (a484a6729e14996c0af1) is added to the log for $logger->info('test')

use Monolog\Logger;
use Monolog\Processor\UidProcessor;
Route::get('/test', function () {
    $logger = new Logger('main');
    $logger->pushProcessor(new UidProcessor(20)); 
    $logger->info('test'); // output = [2020-03-24 04:57:26] main.INFO: test [] {"uid":"a484a6729e14996c0af1"}
});


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2QFYgNC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire