Skip to content

Commit

Permalink
Add forwarding close methods to several writer implementations (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbuell authored Jan 13, 2024
1 parent c1ab4ed commit 4d78dc5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"reflect"
"testing"

"github.com/rs/zerolog/internal/cbor"
)

func TestCtx(t *testing.T) {
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion diode/diode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4d78dc5

Please sign in to comment.