go-sql-driver/mysql · warning

logger is nil

Error message

logger is nil

What it means

Returned by mysql.SetLogger (errors.go:55-58) when the caller passes nil. The driver needs a non-nil logger to report critical internal errors, so it rejects the call and leaves the existing defaultLogger unchanged.

Source

Thrown at errors.go:57

var defaultLogger = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime))

// Logger is used to log critical error messages.
type Logger interface {
	Print(v ...any)
}

// NopLogger is a nop implementation of the Logger interface.
type NopLogger struct{}

// Print implements Logger interface.
func (nl *NopLogger) Print(_ ...any) {}

// SetLogger is used to set the default logger for critical errors.
// The initial logger is os.Stderr.
func SetLogger(logger Logger) error {
	if logger == nil {
		return errors.New("logger is nil")
	}
	defaultLogger = logger
	return nil
}

// MySQLError is an error type which represents a single MySQL error
type MySQLError struct {
	Number   uint16
	SQLState [5]byte
	Message  string
}

func (me *MySQLError) Error() string {
	if me.SQLState != [5]byte{} {
		return fmt.Sprintf("Error %d (%s): %s", me.Number, me.SQLState, me.Message)
	}

	return fmt.Sprintf("Error %d: %s", me.Number, me.Message)

View on GitHub (pinned to c426bd9379)

Solutions

  1. Pass a real logger (e.g. log.New(os.Stderr, ...) or your structured logger adapted to the mysql.Logger interface).
  2. To silence output explicitly, pass mysql.NopLogger{} instead of nil.
  3. Guard the call site: if l != nil { mysql.SetLogger(l) }.

Example fix

// before
mysql.SetLogger(nil)
// after
mysql.SetLogger(mysql.NopLogger{}) // or a real *log.Logger
Defensive patterns

Strategy: validation

Validate before calling

if l == nil {
    l = mysql.NopLogger{}
}
if err := mysql.SetLogger(l); err != nil {
    return err
}

Type guard

func nonNilLogger(l mysql.Logger) mysql.Logger { if l == nil { return mysql.NopLogger{} }; return l }

Try / catch

if err := mysql.SetLogger(l); err != nil { log.Print(err) }

Prevention

When it happens

Trigger: Calling mysql.SetLogger(nil) directly. The guard at errors.go:56 returns errors.New("logger is nil").

Common situations: Conditionally wiring a logger and passing nil when it was unavailable; refactoring that inadvertently forwards a nil pointer; tests that try to silence output by passing nil.

Related errors


AI-assisted analysis of go-sql-driver/mysql@c426bd9379 (2026-08-04). Data as JSON: /data/errors/dabe3e5475d3d6a1.json. Report an issue: GitHub.