{"id":"dabe3e5475d3d6a1","repo":"go-sql-driver/mysql","slug":"logger-is-nil","errorCode":null,"errorMessage":"logger is nil","messagePattern":"logger is nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"errors.go","lineNumber":57,"sourceCode":"\nvar defaultLogger = Logger(log.New(os.Stderr, \"[mysql] \", log.Ldate|log.Ltime))\n\n// Logger is used to log critical error messages.\ntype Logger interface {\n\tPrint(v ...any)\n}\n\n// NopLogger is a nop implementation of the Logger interface.\ntype NopLogger struct{}\n\n// Print implements Logger interface.\nfunc (nl *NopLogger) Print(_ ...any) {}\n\n// SetLogger is used to set the default logger for critical errors.\n// The initial logger is os.Stderr.\nfunc SetLogger(logger Logger) error {\n\tif logger == nil {\n\t\treturn errors.New(\"logger is nil\")\n\t}\n\tdefaultLogger = logger\n\treturn nil\n}\n\n// MySQLError is an error type which represents a single MySQL error\ntype MySQLError struct {\n\tNumber   uint16\n\tSQLState [5]byte\n\tMessage  string\n}\n\nfunc (me *MySQLError) Error() string {\n\tif me.SQLState != [5]byte{} {\n\t\treturn fmt.Sprintf(\"Error %d (%s): %s\", me.Number, me.SQLState, me.Message)\n\t}\n\n\treturn fmt.Sprintf(\"Error %d: %s\", me.Number, me.Message)","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/errors.go#L39-L75","documentation":"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.","triggerScenarios":"Calling mysql.SetLogger(nil) directly. The guard at errors.go:56 returns errors.New(\"logger is nil\").","commonSituations":"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.","solutions":["Pass a real logger (e.g. log.New(os.Stderr, ...) or your structured logger adapted to the mysql.Logger interface).","To silence output explicitly, pass mysql.NopLogger{} instead of nil.","Guard the call site: if l != nil { mysql.SetLogger(l) }."],"exampleFix":"// before\nmysql.SetLogger(nil)\n// after\nmysql.SetLogger(mysql.NopLogger{}) // or a real *log.Logger","handlingStrategy":"validation","validationCode":"if l == nil {\n    l = mysql.NopLogger{}\n}\nif err := mysql.SetLogger(l); err != nil {\n    return err\n}","typeGuard":"func nonNilLogger(l mysql.Logger) mysql.Logger { if l == nil { return mysql.NopLogger{} }; return l }","tryCatchPattern":"if err := mysql.SetLogger(l); err != nil { log.Print(err) }","preventionTips":["Pass mysql.NopLogger{} to silence, never nil.","Guard SetLogger calls behind a nil check.","Initialize logging once at startup."],"tags":["logging","config","validation"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}