feat(log): 全面重构日志系统,支持日志级别、去除ANSI乱码并抑制高频轮询

This commit is contained in:
yml2213
2026-09-18 15:04:38 +08:00
parent 20bde97d7a
commit ec76844da8
15 changed files with 415 additions and 100 deletions
+36 -25
View File
@@ -2,10 +2,11 @@ package openlog
import (
"fmt"
"log"
"strings"
"time"
"affiliate_dash/internal/pkg/applog"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
@@ -45,7 +46,7 @@ var Enabled bool
func Init(enabled bool) {
Enabled = enabled
if enabled {
log.Printf("[open] 开放接口调试日志已开启 (OPEN_API_DEBUG)")
applog.Infof("[open] 开放接口调试日志已开启 (OPEN_API_DEBUG)")
}
}
@@ -104,15 +105,17 @@ func APITypeFromSide(side string) string {
}
func IsDebug(c *gin.Context) bool {
if !Enabled {
return false
if applog.IsDebug() || Enabled {
return true
}
if v, ok := c.Get(CtxDebug); ok {
if b, ok := v.(bool); ok {
return b
if c != nil {
if v, ok := c.Get(CtxDebug); ok {
if b, ok := v.(bool); ok {
return b
}
}
}
return Enabled
return false
}
// MaskKey 脱敏 api_key:保留前缀与后 4 位
@@ -145,7 +148,10 @@ func Truncate(s string, max int) string {
return s[:max] + fmt.Sprintf("...(%d bytes)", len(s))
}
func Prefix(c *gin.Context) string {
func Tag(c *gin.Context) string {
if c == nil {
return "open"
}
side, _ := c.Get(CtxSide)
apiType, _ := c.Get(CtxAPIType)
action, _ := c.Get(CtxAction)
@@ -158,32 +164,37 @@ func Prefix(c *gin.Context) string {
} else if sideText != "" {
tag += "." + sideText
}
if actionText != "" {
if actionText != "" && actionText != ActionUnknown {
tag += "." + actionText
}
fields := ""
if apiTypeText != "" {
fields += fmt.Sprintf(" api_type=%s", apiTypeText)
}
if actionText != "" {
fields += fmt.Sprintf(" action=%s", actionText)
}
return fmt.Sprintf("[%s] req_id=%s%s ", tag, GetReqID(c), fields)
return tag
}
func Info(c *gin.Context, format string, args ...interface{}) {
func Prefix(c *gin.Context) string {
return fmt.Sprintf("[%s] req_id=%s ", Tag(c), GetReqID(c))
}
// Debug 输出开放接口调试日志(高频查询、鉴权成功等),在 DEBUG 级别或 OPEN_API_DEBUG 开启时输出
func Debug(c *gin.Context, format string, args ...interface{}) {
if !IsDebug(c) {
return
}
log.Printf(Prefix(c)+format, args...)
applog.Debugf(Prefix(c)+format, args...)
}
// Info 输出开放接口核心业务生命周期事件日志(下单、发货提交、取消、回调等)
func Info(c *gin.Context, format string, args ...interface{}) {
applog.Infof(Prefix(c)+format, args...)
}
// Warn 输出开放接口业务告警/客户端参数错误日志
func Warn(c *gin.Context, format string, args ...interface{}) {
// 鉴权失败等也值得在 debug 时打出
if !Enabled {
return
}
log.Printf(Prefix(c)+format, args...)
applog.Warnf(Prefix(c)+format, args...)
}
// Error 输出开放接口系统故障或下游服务异常日志
func Error(c *gin.Context, format string, args ...interface{}) {
applog.Errorf(Prefix(c)+format, args...)
}
func Elapsed(c *gin.Context) time.Duration {
+1 -1
View File
@@ -15,7 +15,7 @@ func TestPrefixIncludesSideAndAction(t *testing.T) {
SetScope(c, SideSource, ActionPush)
got := Prefix(c)
want := "[open.source_open_v1.push] req_id=req123 api_type=source_open_v1 action=push "
want := "[open.source_open_v1.push] req_id=req123 "
if got != want {
t.Fatalf("prefix mismatch: got %q want %q", got, want)
}