Skip to content

Commit

Permalink
structured logging: indent multi-line string with tab
Browse files Browse the repository at this point in the history
This makes it possible to distinguish between value (indended with tab) and the
final " <<<" delimiter (indended with space).
  • Loading branch information
pohly committed Nov 29, 2021
1 parent bb5d490 commit 6557068
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ func writeStringValue(b *bytes.Buffer, quote bool, v string) {
// Complex multi-line string, show as-is with indention.
b.WriteString(">>>\n")
for index != -1 {
b.WriteByte(' ')
b.WriteByte('\t')
b.Write(data[0 : index+1])
data = data[index+1:]
index = bytes.IndexByte(data, '\n')
Expand All @@ -889,7 +889,7 @@ func writeStringValue(b *bytes.Buffer, quote bool, v string) {
} else {
// No line break at end of last line, write rest of string and
// add one.
b.WriteByte(' ')
b.WriteByte('\t')
b.Write(data)
b.WriteString("\n <<<")
}
Expand Down
16 changes: 8 additions & 8 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@ with a line break.`,
msg: `first message line
second message line`,
format: `I0102 15:04:05.067890 1234 klog_test.go:%d] "first message line\nsecond message line" multiLine>>>
first value line
second value line
first value line
second value line
<<<
`,
keysValues: []interface{}{"multiLine", `first value line
Expand All @@ -983,8 +983,8 @@ second value line`},
{
msg: `message`,
format: `I0102 15:04:05.067890 1234 klog_test.go:%d] "message" myData>>>
{Data:This is some long text
with a line break.}
{Data:This is some long text
with a line break.}
<<<
`,
keysValues: []interface{}{"myData", myData},
Expand Down Expand Up @@ -1109,10 +1109,10 @@ No whitespace.`,
"pod", "kubedns",
},
want: ` multiLineString>>>
Hello world!
Starts with tab.
Starts with spaces.
No whitespace.
Hello world!
Starts with tab.
Starts with spaces.
No whitespace.
<<< pod="kubedns"`,
},
{
Expand Down

0 comments on commit 6557068

Please sign in to comment.