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
This commit is contained in:
parent
d051c161b7
commit
4a2666aa3b
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
2
main.go
2
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue