use App\Logging\Formatter\LogStashFormatter as CustomerLogstashFormatter; use Illuminate\Log\Logger; use Monolog\Formatter\LogstashFormatter as MonoLogstashFormatter; use Monolog\Processor\MemoryUsageProcessor; use Monolog\Processor\ProcessIdProcessor; use Monolog\Processor\WebProcessor;
class LogstashJsonFormatter { /** * 自定义给定的日志实例 * * @param Logger $logger * * @return void */ public function __invoke(Logger $logger) { foreach ($logger->getHandlers() as $handler) { $handler->setFormatter(new CustomerLogstashFormatter(env("APP_NAME"))); $handler->pushProcessor(new WebProcessor()); $handler->pushProcessor(new MemoryUsageProcessor()); $handler->pushProcessor(new ProcessIdProcessor()); // 进行额外扩展数据的添加 $handler->pushProcessor( function ($record) { $record['extra']['request_id'] = REQUEST_ID; return $record; } ); } } }