Wednesday, April 25, 2018

Customoze laravel monolog - add user IP and UUID to logs

Step 1: add custom function to AppServiceProvider

private function addUniqueIdAndIpToMonoLog(){    $ip = \Request::ip();    $uuid = \Uuid::generate();    $monolog = \Log::getMonolog();    $monolog->pushProcessor(function ($record) use ($uuid, $ip) {        $record['extra']['request-id'] = $uuid;        $record['extra']['ip'] = $ip;        return $record;    });
}

Step 2:  Then add is to boot() in AppServiceProvider 

public function boot(){    $this->addUniqueIdAndIpToMonoLog();}

Step 3:  edit app/bootstrap/app/php

$app->configureMonologUsing(function($monolog) use ($app) {    $monolog->pushHandler(        (new Monolog\Handler\RotatingFileHandler(             $app->storagePath().'/logs/sfr-access-log.log'//            $app->make('config')->get('app.log_max_files', 1825)        ))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true, true))    );});
return $app;

These will add user ip and uuid for single request.
Share/Bookmark

0 comments:

Post a Comment