Skip to content

Commit

Permalink
Remove automatic lock in NewCore (#369)
Browse files Browse the repository at this point in the history
Remove the automatic call to `zapcore.Lock` from `zapcore.NewCore` - when using `zapcore`, it's the user's responsibility to lock if necessary. This change allows concurrency-safe `WriteSyncer` implementations to be used without an additional layer of locking.
  • Loading branch information
flisky authored and akshayjshah committed Mar 14, 2017
1 parent 6314854 commit 74ca5ef
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ func Fields(fs ...zapcore.Field) Option {
}

// ErrorOutput sets the destination for errors generated by the logger. The
// supplied WriteSyncer is automatically wrapped with a mutex, so it need not be
// safe for concurrent use.
// supplied WriteSyncer must be safe for concurrent use.
//
// The Open and zapcore.Lock functions are the simplest ways to make files safe
// for concurrent use.
func ErrorOutput(w zapcore.WriteSyncer) Option {
return optionFunc(func(log *Logger) {
log.errorOutput = zapcore.Lock(w)
log.errorOutput = w
})
}

Expand Down
4 changes: 2 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
return writers, close, errs.AsError()
}

// CombineWriteSyncers combines the passed set of WriteSyncer objects into a
// locked WriteSyncer.
// CombineWriteSyncers combines multiple WriteSyncers into a single, locked
// WriteSyncer.
func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer {
if len(writers) == 0 {
return zapcore.AddSync(ioutil.Discard)
Expand Down
2 changes: 1 addition & 1 deletion zapcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewCore(enc Encoder, ws WriteSyncer, enab LevelEnabler) Core {
return &ioCore{
LevelEnabler: enab,
enc: enc,
out: Lock(ws),
out: ws,
}
}

Expand Down

0 comments on commit 74ca5ef

Please sign in to comment.