From 939343b64543e2467e1fadf40d8d43775f628d7d Mon Sep 17 00:00:00 2001 From: Garret Buell Date: Sat, 13 Jan 2024 08:32:26 -0800 Subject: [PATCH 1/3] Decode CBOR in the diode test to fix workflow failures --- diode/diode_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) } From a3175cd2c5a1b7ecc6d08efc2209ae7ff5b53ed3 Mon Sep 17 00:00:00 2001 From: Garret Buell Date: Sat, 13 Jan 2024 08:43:45 -0800 Subject: [PATCH 2/3] Use CBOR decoding in the ctx test which is also failing the workflow --- ctx_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) } } From b904cfc89da18c8558ac2ce2353408efe4fdc925 Mon Sep 17 00:00:00 2001 From: Garret Buell Date: Sat, 13 Jan 2024 08:53:09 -0800 Subject: [PATCH 3/3] Add forwarding close methods to several writer implementations --- console.go | 9 +++++++++ syslog.go | 9 +++++++++ writer.go | 5 +++++ 3 files changed, 23 insertions(+) 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/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 {