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

Add a more-explicit NewNop constructor #326

Merged
merged 1 commit into from
Feb 20, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
//
// Both L and S are unsynchronized, so replacing them while they're in
// use isn't safe.
L = New(nil)
L = NewNop()
// S is a global SugaredLogger, similar to L. It also defaults to a no-op
// implementation.
S = L.Sugar()
Expand Down
16 changes: 15 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package zap

import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -56,7 +57,7 @@ type Logger struct {
// convenient.
func New(core zapcore.Core, options ...Option) *Logger {
if core == nil {
core = zapcore.NewNopCore()
return NewNop()
}
log := &Logger{
core: core,
Expand All @@ -66,6 +67,19 @@ func New(core zapcore.Core, options ...Option) *Logger {
return log.WithOptions(options...)
}

// NewNop returns a no-op Logger. It never writes out logs or internal errors,
// and it never runs user-defined hooks.
//
// Using WithOptions to replace the Core or error output of a no-op Logger can
// re-enable logging.
func NewNop() *Logger {
return &Logger{
core: zapcore.NewNopCore(),
errorOutput: zapcore.AddSync(ioutil.Discard),
addStack: zapcore.FatalLevel + 1,
}
}

// NewProduction builds a sensible production Logger that writes InfoLevel and
// above logs to standard error as JSON.
//
Expand Down