From dec9d251e6f0b6471eaa6744d33edd741e5ee748 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Sat, 18 Jan 2025 17:22:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=AE=9E=E7=8E=B0request=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/driver/Logger.php | 55 +++++++++++++++++++++++++++ app/admin/middleware.php | 1 + app/admin/middleware/v1/RequestId.php | 25 ++++++++++++ config/log.php | 27 ++++++++++++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/admin/driver/Logger.php create mode 100644 app/admin/middleware/v1/RequestId.php diff --git a/app/admin/driver/Logger.php b/app/admin/driver/Logger.php new file mode 100644 index 00000000..c29c271d --- /dev/null +++ b/app/admin/driver/Logger.php @@ -0,0 +1,55 @@ +config['json']) { + $this->config['format'] = '[%s][%s][%s] %s'; + } + + $destination = $this->getMasterLogFile(); + + $path = dirname($destination); + !is_dir($path) && mkdir($path, 0755, true); + + $info = []; + + // 日志信息封装 + $time = \DateTime::createFromFormat('0.u00 U', microtime())->setTimezone(new \DateTimeZone(date_default_timezone_get()))->format($this->config['time_format']); + + $request_id = $log['request_id']; + unset($log['request_id']); + + foreach ($log as $type => $val) { + $message = []; + foreach ($val as $msg) { + if (!is_string($msg)) { + $msg = var_export($msg, true); + } + + $message[] = $this->config['json'] ? + json_encode(['time' => $time, 'type' => $type, 'request_id' => $request_id, 'msg' => $msg], $this->config['json_options']) : + sprintf($this->config['format'], $time, $type, $request_id, $msg); + } + + if (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level'])) { + // 独立记录的日志级别 + $filename = $this->getApartLevelFile($path, $type); + $this->write($message, $filename); + continue; + } + + $info[$type] = $message; + } + + if ($info) { + return $this->write($info, $destination); + } + + return true; + } +} \ No newline at end of file diff --git a/app/admin/middleware.php b/app/admin/middleware.php index cb6a4a9b..afc74238 100644 --- a/app/admin/middleware.php +++ b/app/admin/middleware.php @@ -3,4 +3,5 @@ return [ // 登录验证 app\admin\middleware\v1\Auth::class, + app\admin\middleware\v1\RequestId::class, ]; diff --git a/app/admin/middleware/v1/RequestId.php b/app/admin/middleware/v1/RequestId.php new file mode 100644 index 00000000..bbf75294 --- /dev/null +++ b/app/admin/middleware/v1/RequestId.php @@ -0,0 +1,25 @@ +request_id = $request_id; + header('X-Request-ID: ' . $request_id); + + Event::listen('think\event\LogWrite', function($event) use($request_id) { + $event->log['request_id'] = $request_id; + }); + + + return $next($request); + } +} \ No newline at end of file diff --git a/config/log.php b/config/log.php index 0d406f84..3169c9c9 100644 --- a/config/log.php +++ b/config/log.php @@ -3,9 +3,12 @@ // +---------------------------------------------------------------------- // | 日志设置 // +---------------------------------------------------------------------- + +use think\facade\Log; + return [ // 默认日志记录通道 - 'default' => 'file', + 'default' => 'MyFileLogger', // 日志记录级别 'level' => [], // 日志类型记录的通道 ['error'=>'email',...] @@ -39,6 +42,28 @@ return [ // 是否实时写入 'realtime_write' => false, ], + 'MyFileLogger' => [ + // 日志记录方式 + 'type' => \app\admin\driver\Logger::class, + // 日志保存目录 + 'path' => '', + // 单文件日志写入 + 'single' => false, + // 独立日志级别 + 'apart_level' => [], + // 最大日志文件数量 + 'max_files' => 0, + // 使用JSON格式记录 + 'json' => false, + // 日志处理 + 'processor' => null, + // 关闭通道日志写入 + 'close' => false, + // 日志输出格式化 + 'format' => '[%s][%s][%s] %s', + // 是否实时写入 + 'realtime_write' => false, + ] // 其它日志通道配置 ],