(t *testing.T)
| 71 | } |
| 72 | |
| 73 | func TestRequestLoggerError(t *testing.T) { |
| 74 | old := slog.Default() |
| 75 | t.Cleanup(func() { |
| 76 | slog.SetDefault(old) |
| 77 | }) |
| 78 | |
| 79 | e := echo.New() |
| 80 | buf := new(bytes.Buffer) |
| 81 | e.Logger = slog.New(slog.NewJSONHandler(buf, nil)) |
| 82 | e.Use(RequestLogger()) |
| 83 | |
| 84 | e.GET("/test", func(c *echo.Context) error { |
| 85 | return errors.New("nope") |
| 86 | }) |
| 87 | req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 88 | rec := httptest.NewRecorder() |
| 89 | e.ServeHTTP(rec, req) |
| 90 | |
| 91 | logAttrs := map[string]any{} |
| 92 | assert.NoError(t, json.Unmarshal(buf.Bytes(), &logAttrs)) |
| 93 | logAttrs["latency"] = 123 |
| 94 | logAttrs["time"] = "x" |
| 95 | |
| 96 | expect := map[string]any{ |
| 97 | "level": "ERROR", |
| 98 | "msg": "REQUEST_ERROR", |
| 99 | "method": "GET", |
| 100 | "uri": "/test", |
| 101 | "status": float64(500), |
| 102 | "bytes_in": "", |
| 103 | "host": "example.com", |
| 104 | "bytes_out": float64(36.0), |
| 105 | "user_agent": "", |
| 106 | "remote_ip": "192.0.2.1", |
| 107 | "request_id": "", |
| 108 | "error": "nope", |
| 109 | |
| 110 | "latency": 123, |
| 111 | "time": "x", |
| 112 | } |
| 113 | assert.Equal(t, expect, logAttrs) |
| 114 | } |
| 115 | |
| 116 | func TestRequestLoggerWithConfig(t *testing.T) { |
| 117 | e := echo.New() |
nothing calls this directly
no test coverage detected
searching dependent graphs…