Search code examples
phplaravellogginglaravel-10

Laravel contextual information logging doesn't work when specifying logging channel


When using contextual information if logging in Laravel v10 (https://laravel.com/docs/10.x/logging#contextual-information), the contextual information doesn't get merged when specifying the channel name.

e.g.: This works fine:

Log::info(
   'User {user} created Thing {id}', 
   [
      'user' => auth()->user()->id,
      'id' => $thing->id
   ]
);

...produces:

[2024-01-31 08:44:23] local.INFO: User 1 created Thing 3 {"user":1,"id":3} 

However, this doesn't work:

Log::channel('mychannel')->info(
   'User {user} created Thing {id}',
   [
      'user' => auth()->user()->id,
      'id' => $thing->id
   ]
);

...as it produces:

[2024-01-31 08:36:15] local.INFO: User {user} created Thing {id} {"user":1,"id":2} 

Any idea what I'm doing wrong?


Solution

  • Whilst not covered in the documentation - in order to get this to work, you need to include the 'replace_placeholders' => true directive in the logging config, e.g.:

    config/logging.php:

    <?php
    ...
            'mychannel' => [
                'driver' => 'single',
                'path' => storage_path('logs/mychannel.log'),
                'level' => 'debug',
                'replace_placeholders' => true,
            ],