docs: LogBind 示例更新为入口统一绑定口径

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-24 01:40:34 +08:00
parent 332c2414bc
commit a0719dc72d
+14 -7
View File
@@ -34,17 +34,24 @@ HoTime 框架内置 Seq 日志推送支持。通过在 `config.json` 填写 `seq
框架在 `handler` 入口派生请求级 Logger,并浅拷贝 `Db` 将其 `Log` 指向同一 Logger,因此 **SQL 日志自动带会话字段**。字段会出现在同条日志的控制台/文件/Seq 出口上。
业务在 `SetConnectListener` 鉴权通过后绑定xbc `main.go`):
业务在 `SetConnectListener` **入口处统一绑定**xbc `main.go`,置于各模块守卫之前,未登录被拒的 Warning 也能带上 shop_id/platform 等维度):
```go
// app 鉴权通过后
context.LogBind("user_id", context.Session("user_id").ToCeilInt64())
// admin 鉴权通过后(或 session 已有 admin_id
context.LogBind("admin_id", context.Session("admin_id").ToCeilInt64())
// 仅对 API 路由(3 段且非静态资源)生效
if len(context.RouterString) == 3 && !strings.Contains(context.RouterString[2], ".") {
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)
}
```
`LogBind` 后,本请求后续业务日志与 SQL 日志都会带上该字段。
`LogBind` 后,本请求后续业务日志与 SQL 日志都会带上该字段。session 在 context 内有缓存,多次 `Session()` 只查一次库。
**不带会话字段的边界:**