Skip to content

Commit

Permalink
fix serialization of special html chars
Browse files Browse the repository at this point in the history
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
  • Loading branch information
dims committed Apr 15, 2020
1 parent 99c45d9 commit db06a1b
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 db06a1b

Please sign in to comment.