Skip to content

Commit

Permalink
Logger disable HTML escaping (#4828)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Dye <[email protected]>
  • Loading branch information
andrewwdye authored Feb 5, 2024
1 parent 2a120f4 commit 05233f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flytestdlib/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func onConfigUpdated(cfg Config) {
default:
if _, isJSON := logrus.StandardLogger().Formatter.(*logrus.JSONFormatter); !isJSON {
logrus.SetFormatter(&logrus.JSONFormatter{
DataKey: jsonDataKey,
DataKey: jsonDataKey,
DisableHTMLEscape: true,
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "ts",
},
Expand Down
34 changes: 34 additions & 0 deletions flytestdlib/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ package logger
import (
"context"
"reflect"
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func init() {
initLogger()
}

func initLogger() {
if err := SetConfig(&Config{
Level: InfoLevel,
IncludeSourceCode: true,
Expand Down Expand Up @@ -585,4 +591,32 @@ func Test_onConfigUpdated(t *testing.T) {
onConfigUpdated(tt.args.cfg)
})
}
initLogger()
}

type loggerHook struct {
messages []string
}

func (h *loggerHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *loggerHook) Fire(entry *logrus.Entry) error {
s, err := entry.String()
if err != nil {
panic(err)
}
h.messages = append(h.messages, s)
return nil
}

func TestConfigFormatterJson_DontEscapeHTML(t *testing.T) {
ctx := context.TODO()
hook := &loggerHook{}
logrus.AddHook(hook)

Info(ctx, "foo&bar")
require.Len(t, hook.messages, 1)
assert.True(t, strings.Contains(hook.messages[0], "foo&bar"), hook.messages[0])
}

0 comments on commit 05233f9

Please sign in to comment.