From 4be34c6b75b457dee1d6658c26f63c228cb8ae8a Mon Sep 17 00:00:00 2001 From: Yin Jifeng Date: Mon, 13 Mar 2017 11:22:23 +0800 Subject: [PATCH 1/2] remove WriteSyncer's automatic lock The lock goes up to function call to make changes compatible. And now, it's user's duty to make WriteSyncer concurrent-safe. --- config.go | 4 ++-- options.go | 2 +- writer.go | 2 +- zapcore/core.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 099d1b34c..625cb5c61 100644 --- a/config.go +++ b/config.go @@ -162,8 +162,8 @@ func (cfg Config) Build(opts ...Option) (*Logger, error) { } log := New( - zapcore.NewCore(enc, sink, cfg.Level), - cfg.buildOptions(errSink)..., + zapcore.NewCore(enc, zapcore.Lock(sink), cfg.Level), + cfg.buildOptions(zapcore.Lock(errSink))..., ) if len(opts) > 0 { log = log.WithOptions(opts...) diff --git a/options.go b/options.go index 29fdbf84e..43c5ae383 100644 --- a/options.go +++ b/options.go @@ -63,7 +63,7 @@ func Fields(fs ...zapcore.Field) Option { // safe for concurrent use. func ErrorOutput(w zapcore.WriteSyncer) Option { return optionFunc(func(log *Logger) { - log.errorOutput = zapcore.Lock(w) + log.errorOutput = w }) } diff --git a/writer.go b/writer.go index 121db9826..0ecacb439 100644 --- a/writer.go +++ b/writer.go @@ -81,5 +81,5 @@ func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer { if len(writers) == 0 { return zapcore.AddSync(ioutil.Discard) } - return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...)) + return zapcore.NewMultiWriteSyncer(writers...) } diff --git a/zapcore/core.go b/zapcore/core.go index bf33b1af3..c8e59eb3d 100644 --- a/zapcore/core.go +++ b/zapcore/core.go @@ -61,7 +61,7 @@ func NewCore(enc Encoder, ws WriteSyncer, enab LevelEnabler) Core { return &ioCore{ LevelEnabler: enab, enc: enc, - out: Lock(ws), + out: ws, } } From 35bc23ee3ee1d840fc5a0a6b64794016fb90befb Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Mon, 13 Mar 2017 08:53:18 -0700 Subject: [PATCH 2/2] Keep some auto-locking --- config.go | 4 ++-- options.go | 6 ++++-- writer.go | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 625cb5c61..099d1b34c 100644 --- a/config.go +++ b/config.go @@ -162,8 +162,8 @@ func (cfg Config) Build(opts ...Option) (*Logger, error) { } log := New( - zapcore.NewCore(enc, zapcore.Lock(sink), cfg.Level), - cfg.buildOptions(zapcore.Lock(errSink))..., + zapcore.NewCore(enc, sink, cfg.Level), + cfg.buildOptions(errSink)..., ) if len(opts) > 0 { log = log.WithOptions(opts...) diff --git a/options.go b/options.go index 43c5ae383..c80146eec 100644 --- a/options.go +++ b/options.go @@ -59,8 +59,10 @@ 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 = w diff --git a/writer.go b/writer.go index 0ecacb439..238ca6f36 100644 --- a/writer.go +++ b/writer.go @@ -75,11 +75,11 @@ 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) } - return zapcore.NewMultiWriteSyncer(writers...) + return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...)) }