MCPcopy
hub / github.com/go-gorm/gorm / Trace

Method Trace

logger/slog.go:58–94  ·  view source on GitHub ↗
(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Source from the content-addressed store, hash-verified

56}
57
58func (l *slogLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
59 if l.LogLevel <= Silent {
60 return
61 }
62
63 elapsed := time.Since(begin)
64 sql, rows := fc()
65 fields := []slog.Attr{
66 slog.String("duration", fmt.Sprintf("%.3fms", float64(elapsed.Nanoseconds())/1e6)),
67 slog.String("sql", sql),
68 }
69
70 if rows != -1 {
71 fields = append(fields, slog.Int64("rows", rows))
72 }
73
74 switch {
75 case err != nil && (!l.IgnoreRecordNotFoundError || !errors.Is(err, ErrRecordNotFound)):
76 fields = append(fields, slog.String("error", err.Error()))
77 l.log(ctx, slog.LevelError, "SQL executed", slog.Attr{
78 Key: "trace",
79 Value: slog.GroupValue(fields...),
80 })
81
82 case l.SlowThreshold != 0 && elapsed > l.SlowThreshold:
83 l.log(ctx, slog.LevelWarn, "SQL executed", slog.Attr{
84 Key: "trace",
85 Value: slog.GroupValue(fields...),
86 })
87
88 case l.LogLevel >= Info:
89 l.log(ctx, slog.LevelInfo, "SQL executed", slog.Attr{
90 Key: "trace",
91 Value: slog.GroupValue(fields...),
92 })
93 }
94}
95
96func (l *slogLogger) log(ctx context.Context, level slog.Level, msg string, args ...any) {
97 if ctx == nil {

Callers

nothing calls this directly

Calls 3

logMethod · 0.95
ErrorMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected