Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b951ba027e | |||
| a0719dc72d |
+14
-7
@@ -34,17 +34,24 @@ HoTime 框架内置 Seq 日志推送支持。通过在 `config.json` 填写 `seq
|
|||||||
|
|
||||||
框架在 `handler` 入口派生请求级 Logger,并浅拷贝 `Db` 将其 `Log` 指向同一 Logger,因此 **SQL 日志自动带会话字段**。字段会出现在同条日志的控制台/文件/Seq 出口上。
|
框架在 `handler` 入口派生请求级 Logger,并浅拷贝 `Db` 将其 `Log` 指向同一 Logger,因此 **SQL 日志自动带会话字段**。字段会出现在同条日志的控制台/文件/Seq 出口上。
|
||||||
|
|
||||||
业务在 `SetConnectListener` 鉴权通过后绑定(xbc `main.go`):
|
业务在 `SetConnectListener` **入口处统一绑定**(xbc `main.go`,置于各模块守卫之前,未登录被拒的 Warning 也能带上 shop_id/platform 等维度):
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// app 鉴权通过后
|
// 仅对 API 路由(3 段且非静态资源)生效
|
||||||
context.LogBind("user_id", context.Session("user_id").ToCeilInt64())
|
if len(context.RouterString) == 3 && !strings.Contains(context.RouterString[2], ".") {
|
||||||
|
if v := context.Session("user_id").ToCeilInt64(); v > 0 {
|
||||||
// admin 鉴权通过后(或 session 已有 admin_id)
|
context.LogBind("user_id", v)
|
||||||
context.LogBind("admin_id", context.Session("admin_id").ToCeilInt64())
|
}
|
||||||
|
// 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()` 只查一次库。
|
||||||
|
|
||||||
**不带会话字段的边界:**
|
**不带会话字段的边界:**
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// isInfrastructureFile 的入参是 shortenPath 后的短路径(最后两段)
|
||||||
|
func TestIsInfrastructureFile(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
file string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
// hotime 框架的 session/context 属中间层,应跳过
|
||||||
|
{"hotime@v1.7.302/session.go", true},
|
||||||
|
{"hotimev1.5/session.go", true},
|
||||||
|
{"hotime@v1.7.302/context.go", true},
|
||||||
|
// 框架基础设施目录
|
||||||
|
{"db/db.go", true},
|
||||||
|
{"cache/cache_db.go", true},
|
||||||
|
{"zerolog/log.go", true},
|
||||||
|
// hotime 根包其余文件不跳过(application.go、code.go 的日志是框架自身行为)
|
||||||
|
{"hotime@v1.7.302/application.go", false},
|
||||||
|
{"hotime@v1.7.302/code.go", false},
|
||||||
|
// 业务侧同名文件不受影响(路径不含 hotime)
|
||||||
|
{"wx/session.go", false},
|
||||||
|
{"app/user.go", false},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := isInfrastructureFile(c.file); got != c.want {
|
||||||
|
t.Errorf("isInfrastructureFile(%q) = %v, want %v", c.file, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -890,6 +890,11 @@ func isInfrastructureFile(file string) bool {
|
|||||||
if strings.HasSuffix(file, "/context.go") || strings.HasSuffix(file, "\\context.go") {
|
if strings.HasSuffix(file, "/context.go") || strings.HasSuffix(file, "\\context.go") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
// session.go 是 session 存取中间层,永远不是业务发起点;
|
||||||
|
// 跳过后 session 读写触发的 SQL 日志 caller 直接落业务帧(而非 session.go:29 <- 业务帧)
|
||||||
|
if strings.HasSuffix(file, "/session.go") || strings.HasSuffix(file, "\\session.go") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user