-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
writer: add CombineWriteSyncers #346
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,15 +36,13 @@ import ( | |
// Passing no paths returns a no-op WriteSyncer. The special paths "stdout" and | ||
// "stderr" are interpreted as os.Stdout and os.Stderr, respectively. | ||
func Open(paths ...string) (zapcore.WriteSyncer, func(), error) { | ||
if len(paths) == 0 { | ||
return zapcore.AddSync(ioutil.Discard), func() {}, nil | ||
} | ||
|
||
writers, close, err := open(paths) | ||
if len(writers) == 1 { | ||
return zapcore.Lock(writers[0]), close, err | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...)), close, err | ||
|
||
writer := CombineWriteSyncers(writers...) | ||
return writer, close, nil | ||
} | ||
|
||
func open(paths []string) ([]zapcore.WriteSyncer, func(), error) { | ||
|
@@ -76,3 +74,12 @@ 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. | ||
func CombineWriteSyncers(writers ...zapcore.WriteSyncer) zapcore.WriteSyncer { | ||
if len(writers) == 0 { | ||
return zapcore.AddSync(ioutil.Discard) | ||
} | ||
return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this function; I'd much rather export just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @akshayjshah the whole point of this PR was to get a function in the API that takes interfaces instead of strings as output file paths. Please see my first comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack, my fault - I confused this with the previously-present Since the files/writers are already open, let's rename this to |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,9 @@ type multiWriteSyncer []WriteSyncer | |
// NewMultiWriteSyncer creates a WriteSyncer that duplicates its writes | ||
// and sync calls, much like io.MultiWriter. | ||
func NewMultiWriteSyncer(ws ...WriteSyncer) WriteSyncer { | ||
if len(ws) == 1 { | ||
return ws[0] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs test coverage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, Currently simply verifying that passing just one argument to NewMultiWriteSyncer simply returns the argument back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Excellent. |
||
// Copy to protect against https://github.com/golang/go/issues/7809 | ||
return multiWriteSyncer(append([]WriteSyncer(nil), ws...)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might just return nil here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to return a discarder, purely for consistency with
New
.