From 4d78dc5ffa901e1c2c5905300ea701c9275f8c52 Mon Sep 17 00:00:00 2001 From: Garret Buell Date: Sat, 13 Jan 2024 10:08:26 -0800 Subject: [PATCH] Add forwarding close methods to several writer implementations (#636) --- console.go | 9 +++++++++ ctx_test.go | 4 +++- diode/diode_test.go | 2 +- syslog.go | 9 +++++++++ writer.go | 5 +++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/console.go b/console.go index e8eeaa33..cc6d623e 100644 --- a/console.go +++ b/console.go @@ -155,6 +155,15 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) { return len(p), err } +// Call the underlying writer's Close method if it is an io.Closer. Otherwise +// does nothing. +func (w ConsoleWriter) Close() error { + if closer, ok := w.Out.(io.Closer); ok { + return closer.Close() + } + return nil +} + // writeFields appends formatted key-value pairs to buf. func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) { var fields = make([]string, 0, len(evt)) diff --git a/ctx_test.go b/ctx_test.go index 397e966c..6315cf7a 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -6,6 +6,8 @@ import ( "io" "reflect" "testing" + + "github.com/rs/zerolog/internal/cbor" ) func TestCtx(t *testing.T) { @@ -93,7 +95,7 @@ func Test_InterfaceLogObjectMarshaler(t *testing.T) { withLog.Info().Msg("test") - if got, want := buf.String(), `{"level":"info","obj":{"name":"custom_value","age":29},"message":"test"}`+"\n"; got != want { + if got, want := cbor.DecodeIfBinaryToString(buf.Bytes()), `{"level":"info","obj":{"name":"custom_value","age":29},"message":"test"}`+"\n"; got != want { t.Errorf("got %q, want %q", got, want) } } diff --git a/diode/diode_test.go b/diode/diode_test.go index a8d25076..ab6b55e3 100644 --- a/diode/diode_test.go +++ b/diode/diode_test.go @@ -70,7 +70,7 @@ func TestFatal(t *testing.T) { } want := "{\"level\":\"fatal\",\"message\":\"test\"}\n" - got := string(slurp) + got := cbor.DecodeIfBinaryToString(slurp) if got != want { t.Errorf("Diode Fatal Test failed. got:%s, want:%s!", got, want) } diff --git a/syslog.go b/syslog.go index c4082830..a2b7285d 100644 --- a/syslog.go +++ b/syslog.go @@ -78,3 +78,12 @@ func (sw syslogWriter) WriteLevel(level Level, p []byte) (n int, err error) { n = len(p) return } + +// Call the underlying writer's Close method if it is an io.Closer. Otherwise +// does nothing. +func (sw syslogWriter) Close() error { + if c, ok := sw.w.(io.Closer); ok { + return c.Close() + } + return nil +} diff --git a/writer.go b/writer.go index 138a612e..41b394d7 100644 --- a/writer.go +++ b/writer.go @@ -27,6 +27,8 @@ func (lw LevelWriterAdapter) WriteLevel(l Level, p []byte) (n int, err error) { return lw.Write(p) } +// Call the underlying writer's Close method if it is an io.Closer. Otherwise +// does nothing. func (lw LevelWriterAdapter) Close() error { if closer, ok := lw.Writer.(io.Closer); ok { return closer.Close() @@ -105,6 +107,9 @@ func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) { return n, err } +// Calls close on all the underlying writers that are io.Closers. If any of the +// Close methods return an error, the remainder of the closers are not closed +// and the error is returned. func (t multiLevelWriter) Close() error { for _, w := range t.writers { if closer, ok := w.(io.Closer); ok {