gormlog

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 8 Imported by: 0

README

gormlog

import "github.com/brpaz/lib-go/db/gormlog"

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

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].
type Logger struct {
    // contains filtered or unexported fields
}

func New
func New(l *logging.Logger, opts ...Option) *Logger

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) Error
func (l *Logger) Error(ctx context.Context, msg string, args ...any)

Error logs a GORM error message.

func (*Logger) Info
func (l *Logger) Info(ctx context.Context, msg string, args ...any)

Info logs an informational GORM message.

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.

func (*Logger) Warn
func (l *Logger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a GORM warning message.

type NopLogger

NopLogger is a [gormlogger.Interface] that discards all output.

type NopLogger struct{}

func NewNopLogger
func NewNopLogger() *NopLogger

NewNopLogger returns a NopLogger.

func (NopLogger) Error
func (NopLogger) Error(context.Context, string, ...any)

func (NopLogger) Info
func (NopLogger) Info(context.Context, string, ...any)

func (NopLogger) LogMode
func (NopLogger) LogMode(gormlogger.LogLevel) gormlogger.Interface

func (NopLogger) Trace
func (NopLogger) Trace(_ context.Context, _ time.Time, _ func() (string, int64), _ error)

func (NopLogger) Warn
func (NopLogger) Warn(context.Context, string, ...any)

type Option

Option configures a Logger.

type Option func(*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
func WithSlowThreshold(d time.Duration) Option

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.

Generated by gomarkdoc

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

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:

func New

func New(l *logging.Logger, opts ...Option) *Logger

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) Error

func (l *Logger) Error(ctx context.Context, msg string, args ...any)

Error logs a GORM error message.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, args ...any)

Info logs an informational GORM message.

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.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a GORM warning message.

type NopLogger

type NopLogger struct{}

NopLogger is a gormlogger.Interface that discards all output.

func NewNopLogger

func NewNopLogger() *NopLogger

NewNopLogger returns a NopLogger.

func (NopLogger) Error

func (NopLogger) Error(context.Context, string, ...any)

func (NopLogger) Info

func (NopLogger) Info(context.Context, string, ...any)

func (NopLogger) LogMode

func (NopLogger) Trace

func (NopLogger) Trace(_ context.Context, _ time.Time, _ func() (string, int64), _ error)

func (NopLogger) Warn

func (NopLogger) Warn(context.Context, string, ...any)

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

func WithSlowThreshold(d time.Duration) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL