From bb5dd2583dc2c2a9fa2e3bb73b498d60e5687c13 Mon Sep 17 00:00:00 2001 From: zhaoyupeng Date: Fri, 18 Jul 2025 09:52:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E4=BA=86=20logger=20?= =?UTF-8?q?ctx=20=E7=9A=84=20key=20feat:=20logger=20trace-value=20?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logger/ctx.go | 19 +++++++++++-------- logger/ctx_test.go | 3 +-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/logger/ctx.go b/logger/ctx.go index 22ca6d0..47b8d47 100644 --- a/logger/ctx.go +++ b/logger/ctx.go @@ -2,17 +2,20 @@ package logger import ( "context" - "gitea.loveuer.com/yizhisec/packages/opt" uuid2 "github.com/google/uuid" ) +type _traceId struct{} + +var TraceId = _traceId{} + func traceId(ctx context.Context) string { if ctx == nil { uuid, _ := uuid2.NewV7() return uuid.String() } - if id, _ := ctx.Value(opt.TraceKey).(string); id != "" { + if id, _ := ctx.Value(TraceId).(string); id != "" { return id } @@ -21,30 +24,30 @@ func traceId(ctx context.Context) string { } func DebugCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Debug(msg, data...) } func InfoCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Info(msg, data...) } func WarnCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Warn(msg, data...) } func ErrorCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Error(msg, data...) } func PanicCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Panic(msg, data...) } func FatalCtx(ctx context.Context, msg string, data ...any) { - msg = "[" + traceId(ctx) + "] " + msg + msg = traceId(ctx) + " | " + msg DefaultLogger.Fatal(msg, data...) } diff --git a/logger/ctx_test.go b/logger/ctx_test.go index f503d1e..8948cac 100644 --- a/logger/ctx_test.go +++ b/logger/ctx_test.go @@ -2,7 +2,6 @@ package logger import ( "context" - "gitea.loveuer.com/yizhisec/packages/opt" "testing" ) @@ -11,7 +10,7 @@ func TestCtxLog(t *testing.T) { InfoCtx(nil, "hello %s", "world") WarnCtx(context.Background(), "hello %s", "world") ctx := context.Background() - ctx = context.WithValue(ctx, opt.TraceKey, "value") + ctx = context.WithValue(ctx, TraceId, "value") SetLogLevel(LogLevelDebug) DebugCtx(ctx, "hello %s", "world") ErrorCtx(ctx, "hello %s", "world")