feat(log): 访问日志携带 ua,挂 Seq 后控制台放宽到 Info+
- WebConnectLog 访问日志新增 ua 字段(User-Agent 按 rune 截断 200 字符),便于按设备归因 - SetSeqWriter 挂 Seq 后业务控制台由 Warn+ 放宽为 Info+,Debug/SQL 仍只进 Seq 与文件 - 新增 Logger.SetConsoleMinLevel,访问日志控制台单独保持 Warn+ Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -199,6 +199,15 @@ func clientIP(req *http.Request) string {
|
||||
return ip
|
||||
}
|
||||
|
||||
// truncateUA 按 rune 安全截断字符串到 max 长度,避免切坏多字节字符导致 JSON 转义问题
|
||||
func truncateUA(s string, max int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= max {
|
||||
return s
|
||||
}
|
||||
return string(r[:max])
|
||||
}
|
||||
|
||||
// shortID 生成 n 字节随机 hex(n=6 → 12 位)
|
||||
func shortID(n int) string {
|
||||
b := make([]byte, n)
|
||||
@@ -466,6 +475,8 @@ func (that *Application) SetConfig(configPath ...string) {
|
||||
that.Log.SetSeqWriter(seqUrl, that.Config.GetString("seqApiKey"), instance)
|
||||
if that.WebConnectLog != nil {
|
||||
that.WebConnectLog.SetSeqWriter(seqUrl, that.Config.GetString("seqApiKey"), instance)
|
||||
// 访问日志量大,挂 Seq 后控制台仍保持 Warn+(业务 Logger 已放宽到 Info+)
|
||||
that.WebConnectLog.SetConsoleMinLevel(zerolog.WarnLevel)
|
||||
}
|
||||
that.Log.Infof("Seq 日志推送已启动: url=%s instance=%s", seqUrl, instance)
|
||||
}
|
||||
@@ -588,6 +599,9 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
|
||||
if country := req.Header.Get("EO-Client-IPCountry"); country != "" {
|
||||
evt = evt.Str("ip_country", country)
|
||||
}
|
||||
if ua := req.Header.Get("User-Agent"); ua != "" {
|
||||
evt = evt.Str("ua", truncateUA(ua, 200))
|
||||
}
|
||||
evt.Msg(context.HandlerStr)
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user