GORM includes a built-in structured logger that records SQL statements, execution time, row counts, and slow query warnings. You can configure it, silence it, or replace it entirely with your own implementation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/go-gorm/gorm/llms.txt
Use this file to discover all available pages before exploring further.
Log levels
Four log levels are defined aslogger.LogLevel constants:
| Level | Constant | Behavior |
|---|---|---|
| 1 | logger.Silent | All logging suppressed |
| 2 | logger.Error | Only errors logged |
| 3 | logger.Warn | Errors and slow query warnings logged |
| 4 | logger.Info | Every SQL statement logged |
Silent < Error < Warn < Info. Anything at or below the configured level is printed.
Default logger
When you do not supply a logger ingorm.Config, GORM uses logger.Default:
os.Stdout with ANSI color codes enabled and warns on queries that take longer than 200ms.
A second built-in logger, logger.Discard, silently drops all log output:
Changing the log level
CallLogMode to get a new logger instance at a different level. The original logger is not modified.
Debug shorthand
db.Debug() creates a new session with Info-level logging enabled for the current statement chain. It does not change the global logger.
Customizing the default logger
Create a new logger withlogger.New, supplying your own Writer and Config:
Logger config fields
Queries that exceed this duration are logged at
Warn level with a SLOW SQL prefix. Set to 0 to disable slow query detection.The minimum severity level that is emitted. One of
Silent, Error, Warn, or Info.When
true, gorm.ErrRecordNotFound is not logged as an error. Useful in applications where a missing record is an expected, non-exceptional condition.When
true, bind parameters are not interpolated into the logged SQL. The logged statement uses ? placeholders instead, which prevents sensitive values from appearing in logs.When
true, ANSI escape codes are added to colorize log output. Disable when writing to a file or a structured log aggregator that does not render terminal colors.Logger interface
Any type implementinglogger.Interface can be used as a GORM logger:
The
Trace method receives a lazy function fc rather than the SQL string directly. Call fc() only when you intend to log the statement to avoid unnecessary string formatting.Implementing a custom logger
The example below routes all GORM log output to azerolog instance:
slog integration
For Go 1.21 and later, GORM ships alogger.NewSlogLogger constructor that wraps *slog.Logger:
| Field | Type | Description |
|---|---|---|
trace.duration | string | Elapsed time in milliseconds ("1.234ms") |
trace.sql | string | The SQL statement |
trace.rows | int64 | Number of rows affected (omitted when -1) |
trace.error | string | Error message, only present on error events |
The
Colorful field in Config has no effect when using NewSlogLogger. Structured log handlers manage their own output formatting.