diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go index 4fa3e9082d..04551fabd0 100644 --- a/pkg/logging/logging.go +++ b/pkg/logging/logging.go @@ -52,12 +52,12 @@ func Info(f string, a ...any) { // Error prints info to stderr but does not end the program func Error(f string, a ...any) { msg := fmt.Sprintf(f, a...) - errorlog.Printf("%s ERROR: %s", formatTs(), msg) + errorlog.Printf("%s: %s", formatTs(), msg) } // Fatal prints info to stderr and ends the program func Fatal(f string, a ...any) { msg := fmt.Sprintf(f, a...) - fatallog.Printf("%s FATAL: %s", formatTs(), msg) + fatallog.Printf("%s: %s", formatTs(), msg) Exit(1) } diff --git a/pkg/shell/writer.go b/pkg/shell/writer.go index ceaa565f8a..1eb61994bd 100644 --- a/pkg/shell/writer.go +++ b/pkg/shell/writer.go @@ -59,16 +59,29 @@ func (w *timestampWriter) Write(p []byte) (n int, err error) { w.startOfLine = false } - // Single atomic write to the underlying writer - nWritten, err := w.writer.Write(buf.Bytes()) - return nWritten, err -} + // Perform the actual write + _, err = w.writer.Write(buf.Bytes()) + + if err != nil { + // If an error occurred, we return 0 bytes of 'p' were written. + // This satisfies the "n < len(p)" rule for errors. + return 0, err + } + return len(p), nil +} func (w *timestampWriter) writeSegment(buf *bytes.Buffer, p []byte) { if w.startOfLine { - ts := time.Now().UTC().Format(time.RFC3339) - coloredTs := logging.TsColor.Sprint(ts) - buf.WriteString(coloredTs + " ") + if len(p) > 0 { + switch p[0] { + case ' ', '\t', '\n': + // Do not add timestamp for indented or empty lines. + default: + ts := time.Now().UTC().Format(time.RFC3339) + coloredTs := logging.TsColor.Sprint(ts) + buf.WriteString(coloredTs + " ") + } + } } buf.Write(p) } diff --git a/pkg/shell/writer_test.go b/pkg/shell/writer_test.go index b49897791a..96ec88d452 100644 --- a/pkg/shell/writer_test.go +++ b/pkg/shell/writer_test.go @@ -29,6 +29,8 @@ func (s *MySuite) TestTimestampWriter(c *C) { testCases := []string{ "line 1\n", + " indented non-timestamped line\n", + "\n", // blank line "line 2 start...", "...line 2 end\n", "line 3",