| 45 | } |
| 46 | |
| 47 | func TestApplicationLogJSONEnabled(t *testing.T) { |
| 48 | var buf bytes.Buffer |
| 49 | _ = NewAccessLogger(Options{ApplicationLogOutput: &buf, ApplicationLogJSONEnabled: true}) |
| 50 | msg := "Hello, world!" |
| 51 | log.Info(msg) |
| 52 | |
| 53 | parsed := make(map[string]interface{}) |
| 54 | err := json.Unmarshal(buf.Bytes(), &parsed) |
| 55 | if err != nil { |
| 56 | t.Errorf("failed to parse json log: %v", err) |
| 57 | } |
| 58 | |
| 59 | if got := parsed["level"]; got != "info" { |
| 60 | t.Errorf("invalid level, expected: info, got: %v", got) |
| 61 | } |
| 62 | |
| 63 | if got := parsed["msg"]; got != msg { |
| 64 | t.Errorf("invalid msg, expected: %s, got: %v", msg, got) |
| 65 | } |
| 66 | |
| 67 | if got, ok := parsed["time"]; ok { |
| 68 | _, err := time.Parse(time.RFC3339, got.(string)) |
| 69 | if err != nil { |
| 70 | t.Errorf("failed to parse time: %v", err) |
| 71 | } |
| 72 | } else { |
| 73 | t.Error("time is missing") |
| 74 | } |
| 75 | |
| 76 | if len(parsed) != 3 { |
| 77 | t.Errorf("unexpected field count") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestApplicationLogJSONWithCustomFormatter(t *testing.T) { |
| 82 | var buf bytes.Buffer |