Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NopLogger: Fix nil Clock panic and release #974

Merged
merged 2 commits into from Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog

## 1.18.1 (28 Jun 2021)

Bugfixes:
* [#974][]: Fix nil dereference in logger constructed by `zap.NewNop`.

[#974]: https://github.com/uber-go/zap/pull/974

## 1.18.0 (28 Jun 2021)

Enhancements:
Expand Down
1 change: 1 addition & 0 deletions logger.go
Expand Up @@ -87,6 +87,7 @@ func NewNop() *Logger {
core: zapcore.NewNopCore(),
errorOutput: zapcore.AddSync(ioutil.Discard),
addStack: zapcore.FatalLevel + 1,
clock: zapcore.DefaultClock,
}
}

Expand Down
22 changes: 21 additions & 1 deletion logger_test.go
Expand Up @@ -556,7 +556,6 @@ func TestLoggerCustomOnFatal(t *testing.T) {
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
withLogger(t, InfoLevel, opts(OnFatal(tt.onFatal)), func(logger *Logger, logs *observer.ObservedLogs) {

var finished bool
recovered := make(chan interface{})
go func() {
Expand All @@ -580,6 +579,27 @@ func TestLoggerCustomOnFatal(t *testing.T) {
}
}

func TestNopLogger(t *testing.T) {
logger := NewNop()

t.Run("basic levels", func(t *testing.T) {
logger.Debug("foo", String("k", "v"))
logger.Info("bar", Int("x", 42))
logger.Warn("baz", Strings("ks", []string{"a", "b"}))
logger.Error("qux", Error(errors.New("great sadness")))
})

t.Run("DPanic", func(t *testing.T) {
logger.With(String("component", "whatever")).DPanic("stuff")
})

t.Run("Panic", func(t *testing.T) {
assert.Panics(t, func() {
logger.Panic("great sadness")
}, "Nop logger should still cause panics.")
})
}

func infoLog(logger *Logger, msg string, fields ...Field) {
logger.Info(msg, fields...)
}
Expand Down