Skip to content

Commit db4fd8f

Browse files
author
Datanoise
committed
logger: initialise L to a no-op SugaredLogger by default
Production code calls Init() from main, but tests and any other entry point that imports the logger package without initialising it would hit a nil-pointer panic on the first L.Warnf / L.Infow call. Default to zap.NewNop().Sugar() so the global is always safe; Init() still replaces it with the configured logger.
1 parent 6d22922 commit db4fd8f

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

logger/logger.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"go.uber.org/zap/zapcore"
88
)
99

10-
var L *zap.SugaredLogger
10+
// L is the global sugared logger. It is initialised to a no-op so that any
11+
// code path which calls into it before Init() (notably unit tests) does not
12+
// nil-panic. Init() replaces it with the configured logger.
13+
var L = zap.NewNop().Sugar()
1114

1215
func Init(level string, jsonFormat bool, logFile string) (*zap.Logger, error) {
1316
var zapLevel zapcore.Level

0 commit comments

Comments
 (0)