From c8382200a0eeef4669612a36902177795abda258 Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Fri, 24 Jul 2026 01:52:35 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20Seq=20LogBind=20=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=9A=E5=8A=A1=E9=A1=B9=E7=9B=AE=E7=BB=86?= =?UTF-8?q?=E8=8A=82=EF=BC=8C=E4=BF=9D=E6=8C=81=E6=A1=86=E6=9E=B6=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E5=8F=A3=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- docs/Seq_日志集成.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/docs/Seq_日志集成.md b/docs/Seq_日志集成.md index 9dcb345..3cc1777 100644 --- a/docs/Seq_日志集成.md +++ b/docs/Seq_日志集成.md @@ -34,24 +34,19 @@ HoTime 框架内置 Seq 日志推送支持。通过在 `config.json` 填写 `seq 框架在 `handler` 入口派生请求级 Logger,并浅拷贝 `Db` 将其 `Log` 指向同一 Logger,因此 **SQL 日志自动带会话字段**。字段会出现在同条日志的控制台/文件/Seq 出口上。 -业务在 `SetConnectListener` **入口处统一绑定**(xbc `main.go`,置于各模块守卫之前,未登录被拒的 Warning 也能带上 shop_id/platform 等维度): +业务在 `SetConnectListener` 中按需绑定自定义字段(建议放在鉴权/守卫之前,这样未登录被拒的 Warning 也能带上维度字段): ```go -// 仅对 API 路由(3 段且非静态资源)生效 -if len(context.RouterString) == 3 && !strings.Contains(context.RouterString[2], ".") { +appIns.SetConnectListener(func(context *Context) bool { if v := context.Session("user_id").ToCeilInt64(); v > 0 { context.LogBind("user_id", v) } - // wechat_id / worker_id / admin_id 同理,session 有则绑 - if shopId := context.ReqData("shop_id").ToCeilInt64(); shopId != 0 { - context.LogBind("shop_id", shopId) - } - // 设备维度(UA / 自定义 header 解析),非空才绑 - // context.LogBind("platform", platform) / context.LogBind("device_model", deviceModel) -} + // 其他 session / 请求参数字段同理:有值再绑 + return false +}) ``` -`LogBind` 后,本请求后续业务日志与 SQL 日志都会带上该字段。session 在 context 内有缓存,多次 `Session()` 只查一次库。 +`LogBind` 后,本请求后续业务日志与 SQL 日志都会带上该字段。session 在 context 内有缓存,多次 `Session()` 只查一次库。字段名与取值由业务自行约定。 **不带会话字段的边界:**