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

ε祈祈猫儿з 提交于 2019-12-02 07:41:33

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;
                });
            }
        }
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!