From 766b76645137a51e0e5869d6e12904cf6d323d35 Mon Sep 17 00:00:00 2001 From: John Eikenberry Date: Wed, 27 Oct 2021 15:18:18 -0700 Subject: [PATCH 1/2] test log writer's reported bytes written count Test that the log writers report their bytes written as the length of the input bytes (on success). --- logging/logging_test.go | 13 ++++++++++--- logging/syslog_test.go | 8 +++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/logging/logging_test.go b/logging/logging_test.go index 08dbc5af0..cf291a7cc 100644 --- a/logging/logging_test.go +++ b/logging/logging_test.go @@ -58,11 +58,18 @@ func TestWriter(t *testing.T) { config := newConfig(&buf) writer, err := newWriter(config) if err != nil { - t.Fatal(err) + t.Error(err) + } + n, err := writer.Write([]byte(tc.input)) + if err != nil { + t.Error(err) + } + if n != len(tc.input) { + t.Errorf("byte count (%d) doesn't match output len (%d).", + n, len(tc.input)) } - writer.Write([]byte(tc.input)) if buf.String() != tc.output { - t.Fatalf("unexpected log output string: '%s'", buf.String()) + t.Errorf("unexpected log output string: '%s'", buf.String()) } } diff --git a/logging/syslog_test.go b/logging/syslog_test.go index 820c5e04c..768b1a1f3 100644 --- a/logging/syslog_test.go +++ b/logging/syslog_test.go @@ -33,13 +33,19 @@ func TestSyslogFilter(t *testing.T) { } s := &SyslogWrapper{l, filt} - n, err := s.Write([]byte("[INFO] test")) + infotest := []byte("[INFO] test") + n, err := s.Write(infotest) if err != nil { t.Fatalf("err: %s", err) } if n == 0 { t.Fatalf("should have logged") } + if n != len(infotest) { + t.Fatalf("byte count (%d) doesn't match output len (%d).", + n, len(infotest)) + + } n, err = s.Write([]byte("[DEBUG] test")) if err != nil { From 9f4a0edfa2215efcb2609ffa3048d8545385722c Mon Sep 17 00:00:00 2001 From: John Eikenberry Date: Wed, 27 Oct 2021 15:18:38 -0700 Subject: [PATCH 2/2] fix custom log writer returned bytes written count It was including the prefixed datetime string in the byte count which made it longer than the passed in byte slice and the io.Multiwriter considers that an error and stops processing the writers at that point (skipping the syslog writer). --- logging/logging.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/logging/logging.go b/logging/logging.go index 145b3854e..4d65d42f8 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -32,7 +32,10 @@ func (writer logWriter) Write(bytes []byte) (int, error) { if len(bytes) == 0 { return 0, nil } - return fmt.Fprintf(writer.out, "%s %s", now(), bytes) + if _, err := fmt.Fprintf(writer.out, "%s %s", now(), bytes); err != nil { + return 0, err + } + return len(bytes), nil } // Config is the configuration for this log setup.