MCPcopy Create free account
hub / github.com/zalando/skipper / TestApplicationLogJSONWithCustomFormatter

Function TestApplicationLogJSONWithCustomFormatter

logging/log_test.go:81–123  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

79}
80
81func TestApplicationLogJSONWithCustomFormatter(t *testing.T) {
82 var buf bytes.Buffer
83 _ = NewAccessLogger(Options{
84 ApplicationLogOutput: &buf,
85 ApplicationLogJSONEnabled: true,
86 ApplicationLogJsonFormatter: &log.JSONFormatter{
87 FieldMap: log.FieldMap{
88 log.FieldKeyLevel: "my_level",
89 log.FieldKeyMsg: "my_message",
90 log.FieldKeyTime: "my_time",
91 },
92 }})
93
94 msg := "Hello, customized world!"
95 log.Info(msg)
96
97 parsed := make(map[string]interface{})
98 err := json.Unmarshal(buf.Bytes(), &parsed)
99 if err != nil {
100 t.Errorf("failed to parse json log: %v", err)
101 }
102
103 if got := parsed["my_level"]; got != "info" {
104 t.Errorf("invalid level, expected: info, got: %v", got)
105 }
106
107 if got := parsed["my_message"]; got != msg {
108 t.Errorf("invalid msg, expected: %s, got: %v", msg, got)
109 }
110
111 if got, ok := parsed["my_time"]; ok {
112 _, err := time.Parse(time.RFC3339, got.(string))
113 if err != nil {
114 t.Errorf("failed to parse time: %v", err)
115 }
116 } else {
117 t.Error("time is missing")
118 }
119
120 if len(parsed) != 3 {
121 t.Errorf("unexpected field count")
122 }
123}
124
125type customFormatter struct {
126 innerFormatter *log.JSONFormatter

Callers

nothing calls this directly

Calls 6

NewAccessLoggerFunction · 0.85
BytesMethod · 0.80
InfoMethod · 0.65
ErrorfMethod · 0.65
ParseMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…