How to add data to all log records in Laravel 5.6?

前端 未结 1 1781
再見小時候
再見小時候 2021-01-22 12:54

How to add data to all log records in Laravel? answers how to add data to all log records in Laravel 5.5 and earlier. e.g. the following is added to AppServiceProvider::register

相关标签:
1条回答
  • 2021-01-22 13:17

    Here is how I solved this.

    1. In .env, use the 'stack channel' for logging (i.e. 'LOG_CHANNEL=stack')
    2. Add a 'tap' to a driver in config\logging.php

      'single' => [
          'driver' => 'single',
          'tap' => [App\Logging\CustomizeFormatter::class],
          'path' => storage_path('logs/laravel.log'),
          'level' => 'debug',
      ]
      
    3. Create App\Logging\CustomizeFormatter:

      namespace App\Logging;
      
      class CustomizeFormatter
      {
          public function __invoke($logger)
          {
              foreach ($logger->getHandlers() as $handler) {
                  $handler->pushProcessor(function ($record) {
                      $record['extra']['ip'] = \Request::getClientIp();
                      $record['extra']['path'] = \Request::path();
      
                      return $record;
                  });
              }
          }
      }
      
    0 讨论(0)
提交回复
热议问题