Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/shell/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ func (w *timestampWriter) Write(p []byte) (n int, err error) {

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 {
firstChar := p[0]
// Avoid printing timestamp if the segment starts with an indentation or newline
if firstChar != ' ' && firstChar != '\t' && firstChar != '\n' {
ts := time.Now().UTC().Format(time.RFC3339)
coloredTs := logging.TsColor.Sprint(ts)
buf.WriteString(coloredTs + " ")
}
}
Comment on lines +69 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and maintainability, consider using a switch statement to check for leading whitespace characters. This makes the logic clearer than a chain of && conditions and is more easily extensible if you need to handle more character types in the future.

                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 + " ")
                        }
                }
References
  1. The style guide emphasizes that code should be maintainable and easy to understand (line 6). Using a switch statement improves readability and maintainability over a complex if condition, which aligns with Go best practices mentioned in the style guide (line 27). (link)

}
buf.Write(p)
}
2 changes: 2 additions & 0 deletions pkg/shell/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading