Skip to content

Commit

Permalink
Merge pull request #140 from dims/fix-serialization-of-special-html-c…
Browse files Browse the repository at this point in the history
…hars

fix serialization of special html chars
  • Loading branch information
k8s-ci-robot committed Apr 15, 2020
2 parents 99c45d9 + db06a1b commit ea583d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions klogr/klogr.go
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"runtime"
"sort"
"strings"

"github.com/go-logr/logr"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -139,8 +140,11 @@ func pretty(value interface{}) string {
value = err.Error()
}
}
jb, _ := json.Marshal(value)
return string(jb)
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.Encode(value)
return strings.TrimSpace(string(buffer.Bytes()))
}

func (l klogger) Info(msg string, kvList ...interface{}) {
Expand Down
6 changes: 6 additions & 0 deletions klogr/klogr_test.go
Expand Up @@ -66,6 +66,12 @@ func TestInfo(t *testing.T) {
text: "test",
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"=null
`,
},
"should correctly html characters": {
text: "test",
keysAndValues: []interface{}{"akey", "<&>"},
expectedOutput: ` "msg"="test" "akey"="<&>"
`,
},
"should correctly handle odd-numbers of KVs in both log values and Info args": {
Expand Down

0 comments on commit ea583d2

Please sign in to comment.