| 79 | } |
| 80 | |
| 81 | func 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 | |
| 125 | type customFormatter struct { |
| 126 | innerFormatter *log.JSONFormatter |