Skip to content

Commit

Permalink
use different delimiters
Browse files Browse the repository at this point in the history
Multi-line strings are now shown as
   fieldName>>>
   <<<
and "| " is used as separator between message and values.

This is shorter:

I1119 13:18:00.210970   78836 structured_logging.go:58] using InfoS | someData={Name:hello Data:world internal:0}
I1119 13:18:00.210988   78836 structured_logging.go:59] using InfoS | longData>>>
 {Name:long Data:Multiple
 lines
 with quite a bit
 of text. internal:0}
 <<<
I1119 13:18:00.211009   78836 structured_logging.go:60] using InfoS with
 the message across multiple lines | int=1 stringData>>>
 long: Multiple
 lines
 with quite a bit
 of text.
 <<< str="another value"
I1119 13:18:00.211032   78836 structured_logging.go:65] using InfoS | logData>>>
 {Name:log output from some program Data:I0000 12:00:00.000000  123456 main.go:42] Starting
 E0000 12:00:01.000000  123456 main.go:43] Failed for some reason
  internal:0}
 <<<
I1119 13:18:00.211055   78836 structured_logging.go:66] using InfoS | boolean=true int=1 float=0.1
I1119 13:18:00.211068   78836 structured_logging.go:71] Did something | item="foobar"
I1119 13:18:00.211079   78836 structured_logging.go:73] This is a full sentence. item="foobar"
  • Loading branch information
pohly committed Nov 19, 2021
1 parent 2ccf744 commit 954847b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,8 @@ func (l *loggingT) printS(err error, s severity, depth int, msg string, keysAndV
// Enhance readability by inserting : between message and key/value
// pairs, but only when the message does not already end with
// punctation.
if len(msg) > 0 && !unicode.IsPunct(rune(msg[len(msg)-1])) {
b.WriteString(":")
if len(msg) > 0 && !unicode.IsPunct(rune(msg[len(msg)-1])) && (err != nil || len(keysAndValues) > 0) {
b.WriteString(" |")
}
if err != nil {
kvListFormat(b, "err", err)
Expand Down Expand Up @@ -861,9 +861,9 @@ func writeStringValue(b *bytes.Buffer, k interface{}, quoteV bool, v string) {
return
}
// Complex multi-line string, show as-is with indention.
b.WriteString(fmt.Sprintf("===start of %s===\n ", k))
b.WriteString(fmt.Sprintf("%s>>>\n ", k))
writeString(b, v)
b.WriteString(fmt.Sprintf("\n ===end of %s===", k))
b.WriteString("\n <<<")
}

func writeString(b *bytes.Buffer, s string) {
Expand Down
30 changes: 15 additions & 15 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,22 +894,22 @@ func TestInfoS(t *testing.T) {
}{
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: pod=\"kubedns\"\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | pod=\"kubedns\"\n",
keysValues: []interface{}{"pod", "kubedns"},
},
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: replicaNum=20\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | replicaNum=20\n",
keysValues: []interface{}{"replicaNum", 20},
},
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: err=\"test error\"\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | err=\"test error\"\n",
keysValues: []interface{}{"err", errors.New("test error")},
},
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: err=\"test error\"\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | err=\"test error\"\n",
keysValues: []interface{}{"err", errors.New("test error")},
},
}
Expand Down Expand Up @@ -956,37 +956,37 @@ with a line break.`,
}{
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: pod=\"kubedns\"\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | pod=\"kubedns\"\n",
keysValues: []interface{}{"pod", "kubedns"},
},
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: replicaNum=20\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | replicaNum=20\n",
keysValues: []interface{}{"replicaNum", 20},
},
{
msg: "test",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test: err=\"test error\"\n",
format: "I0102 15:04:05.067890 1234 klog_test.go:%d] test | err=\"test error\"\n",
keysValues: []interface{}{"err", errors.New("test error")},
},
{
msg: `first message line
second message line`,
format: `I0102 15:04:05.067890 1234 klog_test.go:%d] first message line
second message line: ===start of multiLine===
second message line | multiLine>>>
first value line
second value line
===end of multiLine===
<<<
`,
keysValues: []interface{}{"multiLine", `first value line
second value line`},
},
{
msg: `message`,
format: `I0102 15:04:05.067890 1234 klog_test.go:%d] message: ===start of myData===
format: `I0102 15:04:05.067890 1234 klog_test.go:%d] message | myData>>>
{Data:This is some long text
with a line break.}
===end of myData===
<<<
`,
keysValues: []interface{}{"myData", myData},
},
Expand Down Expand Up @@ -1041,11 +1041,11 @@ func TestErrorS(t *testing.T) {
}{
{
err: fmt.Errorf("update status failed"),
format: "E0102 15:04:05.067890 1234 klog_test.go:%d] Failed to update pod status: err=\"update status failed\" pod=\"kubedns\"\n",
format: "E0102 15:04:05.067890 1234 klog_test.go:%d] Failed to update pod status | err=\"update status failed\" pod=\"kubedns\"\n",
},
{
err: nil,
format: "E0102 15:04:05.067890 1234 klog_test.go:%d] Failed to update pod status: pod=\"kubedns\"\n",
format: "E0102 15:04:05.067890 1234 klog_test.go:%d] Failed to update pod status | pod=\"kubedns\"\n",
},
}
for _, e := range errorList {
Expand Down Expand Up @@ -1109,12 +1109,12 @@ func TestKvListFormat(t *testing.T) {
No whitespace.`,
"pod", "kubedns",
},
want: ` ===start of multiLineString===
want: ` multiLineString>>>
Hello world!
Starts with tab.
Starts with spaces.
No whitespace.
===end of multiLineString=== pod="kubedns"`,
<<< pod="kubedns"`,
},
{
keysValues: []interface{}{"pod", "kubedns", "maps", map[string]int{"three": 4}},
Expand Down

0 comments on commit 954847b

Please sign in to comment.