| 1 |
package dbutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"regexp" |
| 6 |
"runtime" |
| 7 |
"strings" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/rs/zerolog" |
| 11 |
) |
| 12 |
|
| 13 |
type DatabaseLogger interface { |
| 14 |
QueryTiming(ctx context.Context, method, query string, args []any, nrows int, duration time.Duration, err error) |
| 15 |
WarnUnsupportedVersion(current, compat, latest int) |
| 16 |
PrepareUpgrade(current, compat, latest int) |
| 17 |
DoUpgrade(from, to int, message string, txn TxnMode) |
| 18 |
// Deprecated: legacy warning method, return errors instead |
| 19 |
Warn(msg string, args ...any) |
| 20 |
} |
| 21 |
|
| 22 |
type noopLogger struct{} |
| 23 |
|
| 24 |
var NoopLogger DatabaseLogger = &noopLogger{} |
| 25 |
|
| 26 |
func (n noopLogger) WarnUnsupportedVersion(_, _, _ int) {} |
| 27 |
func (n noopLogger) PrepareUpgrade(_, _, _ int) {} |
| 28 |
func (n noopLogger) DoUpgrade(_, _ int, _ string, _ TxnMode) {} |
| 29 |
func (n noopLogger) Warn(msg string, args ...any) {} |
| 30 |
|
| 31 |
func (n noopLogger) QueryTiming(_ context.Context, _, _ string, _ []any, _ int, _ time.Duration, _ error) { |
| 32 |
} |
| 33 |
|
| 34 |
type zeroLogger struct { |
| 35 |
l *zerolog.Logger |
| 36 |
ZeroLogSettings |
| 37 |
} |
| 38 |
|
| 39 |
type ZeroLogSettings struct { |
| 40 |
CallerSkipFrame int |
| 41 |
Caller bool |
| 42 |
|
| 43 |
// TraceLogAllQueries specifies whether or not all queries should be logged |
| 44 |
// at the TRACE level. |
| 45 |
TraceLogAllQueries bool |
| 46 |
} |
| 47 |
|
| 48 |
func ZeroLogger(log zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger { |
| 49 |
return ZeroLoggerPtr(&log, cfg...) |
| 50 |
} |
| 51 |
|
| 52 |
func ZeroLoggerPtr(log *zerolog.Logger, cfg ...ZeroLogSettings) DatabaseLogger { |
| 53 |
wrapped := &zeroLogger{l: log} |
| 54 |
if len(cfg) > 0 { |
| 55 |
wrapped.ZeroLogSettings = cfg[0] |
| 56 |
} else { |
| 57 |
wrapped.ZeroLogSettings = ZeroLogSettings{ |
| 58 |
CallerSkipFrame: 2, // Skip LoggingExecable.ExecContext and zeroLogger.QueryTiming |
| 59 |
Caller: true, |
| 60 |
} |
| 61 |
} |
| 62 |
return wrapped |
| 63 |
} |
| 64 |
|
| 65 |
func (z zeroLogger) WarnUnsupportedVersion(current, compat, latest int) { |
| 66 |
z.l.Warn(). |
| 67 |
Int("current_version", current). |
| 68 |
Int("oldest_compatible_version", compat). |
| 69 |
Int("latest_known_version", latest). |
| 70 |
Msg("Unsupported database schema version, continuing anyway") |
| 71 |
} |
| 72 |
|
| 73 |
func (z zeroLogger) PrepareUpgrade(current, compat, latest int) { |
| 74 |
evt := z.l.Info(). |
| 75 |
Int("current_version", current). |
| 76 |
Int("oldest_compatible_version", compat). |
| 77 |
Int("latest_known_version", latest) |
| 78 |
if current >= latest { |
| 79 |
evt.Msg("Database is up to date") |
| 80 |
} else { |
| 81 |
evt.Msg("Preparing to update database schema") |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func (z zeroLogger) DoUpgrade(from, to int, message string, txn TxnMode) { |
| 86 |
z.l.Info(). |
| 87 |
Int("from", from). |
| 88 |
Int("to", to). |
| 89 |
Str("txn_mode", string(txn)). |
| 90 |
Str("description", message). |
| 91 |
Msg("Upgrading database") |
| 92 |
} |
| 93 |
|
| 94 |
var whitespaceRegex = regexp.MustCompile(`\s+`) |
| 95 |
|
| 96 |
var GlobalSafeQueryLog bool |
| 97 |
|
| 98 |
func (z zeroLogger) QueryTiming(ctx context.Context, method, query string, args []any, nrows int, duration time.Duration, err error) { |
| 99 |
log := zerolog.Ctx(ctx) |
| 100 |
if log.GetLevel() == zerolog.Disabled || log == zerolog.DefaultContextLogger { |
| 101 |
log = z.l |
| 102 |
} |
| 103 |
if (!z.TraceLogAllQueries || log.GetLevel() != zerolog.TraceLevel) && !GlobalSafeQueryLog && duration < 1*time.Second { |
| 104 |
return |
| 105 |
} |
| 106 |
if nrows > -1 { |
| 107 |
rowLog := log.With().Int("rows", nrows).Logger() |
| 108 |
log = &rowLog |
| 109 |
} |
| 110 |
query = strings.TrimSpace(whitespaceRegex.ReplaceAllLiteralString(query, " ")) |
| 111 |
callerSkipFrame := z.CallerSkipFrame |
| 112 |
if GlobalSafeQueryLog || duration > 1*time.Second { |
| 113 |
for ; callerSkipFrame < 10; callerSkipFrame++ { |
| 114 |
_, filename, _, _ := runtime.Caller(callerSkipFrame) |
| 115 |
if !strings.Contains(filename, "/dbutil/") { |
| 116 |
break |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
if GlobalSafeQueryLog { |
| 121 |
log.Debug(). |
| 122 |
Err(err). |
| 123 |
Int64("duration_µs", duration.Microseconds()). |
| 124 |
Str("method", method). |
| 125 |
Str("query", query). |
| 126 |
Caller(callerSkipFrame). |
| 127 |
Msg("Query") |
| 128 |
} else { |
| 129 |
log.Trace(). |
| 130 |
Err(err). |
| 131 |
Int64("duration_µs", duration.Microseconds()). |
| 132 |
Str("method", method). |
| 133 |
Str("query", query). |
| 134 |
Interface("query_args", args). |
| 135 |
Msg("Query") |
| 136 |
} |
| 137 |
if duration >= 1*time.Second { |
| 138 |
evt := log.Warn(). |
| 139 |
Float64("duration_seconds", duration.Seconds()). |
| 140 |
AnErr("result_error", err). |
| 141 |
Str("method", method). |
| 142 |
Str("query", query) |
| 143 |
if z.Caller { |
| 144 |
evt = evt.Caller(callerSkipFrame) |
| 145 |
} |
| 146 |
evt.Msg("Query took long") |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func (z zeroLogger) Warn(msg string, args ...any) { |
| 151 |
z.l.Warn().Msgf(msg, args...) // zerolog-allow-msgf |
| 152 |
} |