0

I have followed instructions to use a monolog processor to add meta data to a log entry like so:

class CustomizeFormatter
{
    public function __invoke($logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->pushProcessor(function ($record) {
                $userid  = Auth::user() ? Auth::user()->id : '';
                $email   = Auth::user() ? Auth::user()->email : '';
                $name    = Auth::user() ? Auth::user()->name : '';

                $req_data = (null !== \Request::all()) ? \Request::all() : '';
                $record['extra']['user_id']  = $userid;
                $record['extra']['email']    = $email;
                $record['extra']['name']     = $name;
                $record['extra']['ip']       = \Request::getClientIp();
                $record['extra']['path']     = \Request::path();
                $record['extra']['action']   = \Request::route()->getActionName();
                $record['extra']['server']   = $_SERVER['SERVER_NAME'];
                $record['extra']['env']      = App::environment();

            return $record;
            });
        }
    }
}

So calling Log::info(['message' =>'my message here']) would produce this.

[2020-03-08 19:50:34] qa.INFO:   array(
    'message' =>'my message here'
)

{ 
    "user_id": 2,
    "email": "user@aol.com",
    "name": "my name",
    "ip": "255.255.255.255",
    "path": "v1/mycall",
    "request": [],
    "env": "qa"
}

is it possible to merge them as one singular json structure like so?

[2020-03-08 19:50:34] qa.INFO:   
{
    "message": "my message here",
    "user_id": 2,
    "email": "user@aol.com",
    "name": "my name",
    "ip": "255.255.255.255",
    "path": "v1/mycall",
    "request": [],
    "env": "qa"
}
user1978109
  • 727
  • 1
  • 8
  • 19
  • If both items are arrays (or possible to be parsed as arrays somehow) - then have you considered using [`array_merge()`](https://www.php.net/manual/en/function.array-merge.php)? – megubyte Mar 08 '20 at 20:35
  • They both start as arrays, one gets converted into json. I can convert the other into json as well with json encode but ideally I would like to use array merge but not sure where that would be done. – user1978109 Mar 08 '20 at 20:44

0 Answers0