refactor(logging): 提取命名常量并适配变量系统重命名
提取 formatJSON 常量替代硬编码 "json" 适配 variable.NewContext/ReleaseContext 重命名 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1ce84ce9c5
commit
3e153f5fe1
@ -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
|
||||
}
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user