From 4a2666aa3b53522dd484fc250b47c214b1ecc914 Mon Sep 17 00:00:00 2001 From: douxu Date: Tue, 19 May 2026 17:38:22 +0800 Subject: [PATCH] fix: correct caller frames in GORM logger and DB arg in main - add *Skip variants (logSkip, makeLogFieldsSkip, getLoggerCallerInfoSkip) so wrapper functions report the true call site, not logger internals - switch GormLogger.Trace to use ErrorSkip/WarnSkip/InfoSkip with extraSkip=1 so SQL log lines point to GORM caller rather than the logger facade - pass postgresDBClient instead of tx to GetFullMeasurementSet in main --- logger/facede.go | 21 ++++++++++++++++++++- logger/gorm_logger.go | 9 ++++----- logger/logger.go | 16 ++++++++++++---- main.go | 2 +- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/logger/facede.go b/logger/facede.go index b9a5b54..1533cdf 100644 --- a/logger/facede.go +++ b/logger/facede.go @@ -39,11 +39,30 @@ func Error(ctx context.Context, msg string, kv ...any) { } func (f *facade) log(ctx context.Context, lvl zapcore.Level, msg string, kv ...any) { - fields := makeLogFields(ctx, kv...) + f.logSkip(ctx, lvl, 0, msg, kv...) +} + +func (f *facade) logSkip(ctx context.Context, lvl zapcore.Level, extraSkip int, msg string, kv ...any) { + fields := makeLogFieldsSkip(ctx, extraSkip, kv...) ce := f._logger.Check(lvl, msg) ce.Write(fields...) } +// ErrorSkip logs at error level with extra caller skip frames for wrapper functions. +func ErrorSkip(ctx context.Context, extraSkip int, msg string, kv ...any) { + logFacade().logSkip(ctx, zapcore.ErrorLevel, extraSkip, msg, kv...) +} + +// WarnSkip logs at warn level with extra caller skip frames for wrapper functions. +func WarnSkip(ctx context.Context, extraSkip int, msg string, kv ...any) { + logFacade().logSkip(ctx, zapcore.WarnLevel, extraSkip, msg, kv...) +} + +// InfoSkip logs at info level with extra caller skip frames for wrapper functions. +func InfoSkip(ctx context.Context, extraSkip int, msg string, kv ...any) { + logFacade().logSkip(ctx, zapcore.InfoLevel, extraSkip, msg, kv...) +} + func logFacade() *facade { fOnce.Do(func() { f = &facade{ diff --git a/logger/gorm_logger.go b/logger/gorm_logger.go index c5e6c01..fadd2e6 100644 --- a/logger/gorm_logger.go +++ b/logger/gorm_logger.go @@ -48,14 +48,13 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql duration := time.Since(begin).Milliseconds() // get gorm exec sql and rows affected sql, rows := fc() - // gorm error judgment if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - Error(ctx, "SQL ERROR", "sql", sql, "rows", rows, "dur(ms)", duration) + ErrorSkip(ctx, 1, "SQL ERROR", "sql", sql, "rows", rows, "dur(ms)", duration) + return } - // slow query judgment if duration > l.SlowThreshold.Milliseconds() { - Warn(ctx, "SQL SLOW", "sql", sql, "rows", rows, "dur(ms)", duration) + WarnSkip(ctx, 1, "SQL SLOW", "sql", sql, "rows", rows, "dur(ms)", duration) } else { - Info(ctx, "SQL INFO", "sql", sql, "rows", rows, "dur(ms)", duration) + InfoSkip(ctx, 1, "SQL INFO", "sql", sql, "rows", rows, "dur(ms)", duration) } } diff --git a/logger/logger.go b/logger/logger.go index 3b4175f..f7fa247 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -47,7 +47,10 @@ func (l *logger) log(lvl zapcore.Level, msg string, kv ...any) { } func makeLogFields(ctx context.Context, kv ...any) []zap.Field { - // Ensure that log information appears in pairs in the form of key-value pairs + return makeLogFieldsSkip(ctx, 0, kv...) +} + +func makeLogFieldsSkip(ctx context.Context, extraSkip int, kv ...any) []zap.Field { if len(kv)%2 != 0 { kv = append(kv, "unknown") } @@ -57,7 +60,7 @@ func makeLogFields(ctx context.Context, kv ...any) []zap.Field { spanID := spanCtx.SpanID().String() kv = append(kv, "traceID", traceID, "spanID", spanID) - funcName, file, line := getLoggerCallerInfo() + funcName, file, line := getLoggerCallerInfoSkip(extraSkip) kv = append(kv, "func", funcName, "file", file, "line", line) fields := make([]zap.Field, 0, len(kv)/2) for i := 0; i < len(kv); i += 2 { @@ -85,9 +88,14 @@ func makeLogFields(ctx context.Context, kv ...any) []zap.Field { return fields } -// getLoggerCallerInfo define func of return log caller information、method name、file name、line number +// getLoggerCallerInfo returns caller info at a fixed depth for the standard facade call chain. func getLoggerCallerInfo() (funcName, file string, line int) { - pc, file, line, ok := runtime.Caller(4) + return getLoggerCallerInfoSkip(0) +} + +// getLoggerCallerInfoSkip returns caller info with additional skip frames beyond the standard depth. +func getLoggerCallerInfoSkip(extraSkip int) (funcName, file string, line int) { + pc, file, line, ok := runtime.Caller(4 + extraSkip) if !ok { return } diff --git a/main.go b/main.go index ef62aae..259b269 100644 --- a/main.go +++ b/main.go @@ -214,7 +214,7 @@ func main() { panic(err) } - measurementSet, err := database.GetFullMeasurementSet(ctx, tx) + measurementSet, err := database.GetFullMeasurementSet(ctx, postgresDBClient) if err != nil { logger.Error(ctx, "generate component measurement group failed", "error", err) panic(err)