From 3e153f5fe1d072349b7c46ad8f8dff21189655c0 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 09:40:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(logging):=20=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E5=B8=B8=E9=87=8F=E5=B9=B6=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=B3=BB=E7=BB=9F=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提取 formatJSON 常量替代硬编码 "json" 适配 variable.NewContext/ReleaseContext 重命名 Co-Authored-By: Claude Opus 4.6 --- internal/logging/logging.go | 14 ++++++++------ internal/logging/logging_test.go | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 4f8bfb8..2e0fae9 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -49,6 +49,8 @@ type AppLogger struct { var log zerolog.Logger +const formatJSON = "json" + // Init 初始化日志系统(兼容旧接口)。 func Init(level string, pretty bool) { l := parseLevel(level) @@ -120,7 +122,7 @@ func LogAccess(ctx *fasthttp.RequestCtx, status int, size int64, duration time.D // LogAccess 记录访问日志,支持模板格式或 JSON。 func (l *Logger) LogAccess(ctx *fasthttp.RequestCtx, status int, size int64, duration time.Duration) { // JSON 格式或空格式:输出结构化 JSON - if l.accessFormat == "json" || l.accessFormat == "" { + if l.accessFormat == formatJSON || l.accessFormat == "" { l.accessLog.Info(). Str("remote_addr", ctx.RemoteAddr().String()). Str("request", string(ctx.Method())+" "+string(ctx.Path())). @@ -170,8 +172,8 @@ func (l *Logger) formatAccessLog(ctx *fasthttp.RequestCtx, status int, size int6 } // 创建变量上下文 - vc := variable.NewVariableContext(ctx) - defer variable.ReleaseVariableContext(vc) + vc := variable.NewContext(ctx) + defer variable.ReleaseContext(vc) // 设置响应信息(同时设置到 ctx 供 builtin getter 使用) vc.SetResponseInfo(status, size, duration.Nanoseconds()) @@ -289,7 +291,7 @@ func NewAppLogger(cfg *config.LoggingConfig) *AppLogger { // LogStartup 记录启动消息。 func (l *AppLogger) LogStartup(msg string, fields map[string]string) { - if l.format == "json" { + if l.format == formatJSON { event := l.errorLog.Info() for k, v := range fields { event.Str(k, v) @@ -315,7 +317,7 @@ func (l *AppLogger) LogStartup(msg string, fields map[string]string) { // LogShutdown 记录停止消息。 func (l *AppLogger) LogShutdown(msg string) { - if l.format == "json" { + if l.format == formatJSON { l.errorLog.Info().Msg(msg) return } @@ -326,7 +328,7 @@ func (l *AppLogger) LogShutdown(msg string) { // LogSignal 记录信号处理消息。 func (l *AppLogger) LogSignal(sig string, action string) { - if l.format == "json" { + if l.format == formatJSON { l.errorLog.Info().Str("signal", sig).Str("action", action).Msg("") return } diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index 3e56616..61c55b5 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -69,7 +69,7 @@ func TestParseLevel(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { result := parseLevel(tt.input) if result != tt.expected { t.Errorf("parseLevel(%q) = %v, want %v", tt.input, result, tt.expected) @@ -90,7 +90,7 @@ func TestNewLogger(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { logger := New(tt.cfg) if logger == nil { t.Error("Expected non-nil Logger") @@ -233,7 +233,7 @@ func TestInit(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { Init(tt.level, tt.pretty) // 验证全局 logger 已初始化 Debug().Msg("test debug") @@ -244,7 +244,7 @@ func TestInit(t *testing.T) { } } -func TestGlobalLogFunctions(t *testing.T) { +func TestGlobalLogFunctions(_ *testing.T) { Init("debug", false) // 测试全局日志函数 @@ -254,7 +254,7 @@ func TestGlobalLogFunctions(t *testing.T) { Error().Str("key", "value").Msg("global error") } -func TestLogAccessGlobal(t *testing.T) { +func TestLogAccessGlobal(_ *testing.T) { Init("info", false) ctx := &fasthttp.RequestCtx{} @@ -348,7 +348,7 @@ func TestNewAppLogger(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { logger := NewAppLogger(tt.cfg) if logger == nil { t.Error("Expected non-nil AppLogger") @@ -370,7 +370,7 @@ func TestAppLoggerLogStartup(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { logger := NewAppLogger(&config.LoggingConfig{Format: tt.format}) logger.LogStartup("server started", tt.fields) }) @@ -387,7 +387,7 @@ func TestAppLoggerLogShutdown(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { logger := NewAppLogger(&config.LoggingConfig{Format: tt.format}) logger.LogShutdown("server stopped") }) @@ -406,14 +406,14 @@ func TestAppLoggerLogSignal(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { logger := NewAppLogger(&config.LoggingConfig{Format: tt.format}) logger.LogSignal(tt.sig, tt.action) }) } } -func TestAppLoggerMethods(t *testing.T) { +func TestAppLoggerMethods(_ *testing.T) { logger := NewAppLogger(&config.LoggingConfig{Format: "json", Error: config.ErrorLogConfig{Level: "debug"}}) logger.Info().Str("test", "value").Msg("app info")