Documentation
¶
Overview ¶
Package gormlog provides gormlogger.Interface adapters for plugging structured loggers into GORM.
Adapters ¶
New wraps a github.com/brpaz/lib-go/logging.Logger. The GORM log level is derived automatically from the logger's current level, keeping query filtering consistent with the rest of the application. Slow-query and trace behaviour can be tuned with WithSlowThreshold and WithIgnoreTrace.
NewNopLogger discards all output. Useful in tests or when GORM logging should be suppressed without touching the slog configuration.
Adding adapters ¶
Any type that implements gorm.io/gorm/logger.Interface can be used as a GORM logger. To add support for another logging library (e.g. zap, logrus), add a new file in this package that implements the four interface methods: LogMode, Info, Warn, Error, and Trace.
Index ¶
- type Logger
- func (l *Logger) Error(ctx context.Context, msg string, args ...any)
- func (l *Logger) Info(ctx context.Context, msg string, args ...any)
- func (l *Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
- func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *Logger) Warn(ctx context.Context, msg string, args ...any)
- type NopLogger
- func (NopLogger) Error(context.Context, string, ...any)
- func (NopLogger) Info(context.Context, string, ...any)
- func (NopLogger) LogMode(gormlogger.LogLevel) gormlogger.Interface
- func (NopLogger) Trace(_ context.Context, _ time.Time, _ func() (string, int64), _ error)
- func (NopLogger) Warn(context.Context, string, ...any)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a GORM gormlogger.Interface adapter backed by a logging.Logger.
The effective log level is derived from the logger at construction via gormLevelFromSlog: logging.Logger.GetLevel is mapped to a GORM level so that filtering stays consistent with the rest of the application.
Logger.LogMode overrides the GORM level (e.g. to silence GORM output independently of the application logger) and returns a shallow clone.
Level mapping:
- Normal queries are emitted at slog.LevelInfo.
- Slow queries are emitted at slog.LevelWarn.
- Errors are emitted at slog.LevelError.
func New ¶
New constructs a Logger backed by the given logging.Logger. The initial GORM log level is derived from the logger's current level.
func (*Logger) LogMode ¶
func (l *Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface
LogMode returns a shallow copy of the logger with the GORM log level set to level. Pass gormlogger.Silent to suppress all output from this logger.
func (*Logger) Trace ¶
func (l *Logger) Trace( ctx context.Context, begin time.Time, fc func() (string, int64), err error, )
Trace logs a SQL query execution. It is called by GORM after every query.
Behaviour:
- Errors (excluding ErrRecordNotFound) are logged at slog.LevelError when logLevel >= Error.
- Queries exceeding [slowThreshold] are logged at slog.LevelWarn when logLevel >= Warn.
- All other queries are logged at slog.LevelInfo when logLevel >= Info and ignoreTrace is unset.
type NopLogger ¶
type NopLogger struct{}
NopLogger is a gormlogger.Interface that discards all output.
func (NopLogger) LogMode ¶
func (NopLogger) LogMode(gormlogger.LogLevel) gormlogger.Interface
type Option ¶
type Option func(*Logger)
Option configures a Logger.
func WithIgnoreTrace ¶
func WithIgnoreTrace() Option
WithIgnoreTrace disables SQL query tracing for non-error, non-slow queries. Errors and slow queries are still logged.
func WithSlowThreshold ¶
WithSlowThreshold sets the duration above which a query is considered slow and logged as a warning. Defaults to 200ms. Set to 0 to disable slow-query logging.