logger = $container->get(LoggerFactory::class)->get('sql'); } public function listen(): array { return [ QueryExecuted::class, ]; } /** * @param object|QueryExecuted $event */ public function process(object $event) { if ($event instanceof QueryExecuted) { $sql = $event->sql; if (!Arr::isAssoc($event->bindings)) { foreach ($event->bindings as $key => $value) { $sql = Str::replaceFirst('?', "'{$value}'", $sql); } } if (env('SQL_QUERY_LOG', false)) { $this->logger->info(sprintf('[%s] %s', $event->time, $sql)); return; } // SQL 监控,通知管理员(大于两秒) if ($event->time >= 2000) { $this->logger->info(sprintf('[%s] %s', $event->time, $sql)); } } } }