(ctx context.Context, status int)
| 141 | } |
| 142 | |
| 143 | func (c *SlogRequestLogger) WriteLog(ctx context.Context, status int) { |
| 144 | if c.written { |
| 145 | return |
| 146 | } |
| 147 | c.written = true |
| 148 | end := time.Now() |
| 149 | |
| 150 | if c.addFields != nil { |
| 151 | c.addFields() |
| 152 | } |
| 153 | |
| 154 | logger := c.log.With( |
| 155 | slog.F("took", end.Sub(c.start)), |
| 156 | slog.F("status_code", status), |
| 157 | slog.F("latency_ms", float64(end.Sub(c.start)/time.Millisecond)), |
| 158 | ) |
| 159 | |
| 160 | // If the request is routed, add the route parameters to the log. |
| 161 | if chiCtx := chi.RouteContext(ctx); chiCtx != nil { |
| 162 | urlParams := chiCtx.URLParams |
| 163 | routeParamsFields := make([]slog.Field, 0, len(urlParams.Keys)) |
| 164 | |
| 165 | for k, v := range urlParams.Keys { |
| 166 | if urlParams.Values[k] != "" { |
| 167 | routeParamsFields = append(routeParamsFields, slog.F("params_"+v, urlParams.Values[k])) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if len(routeParamsFields) > 0 { |
| 172 | logger = logger.With(routeParamsFields...) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // We already capture most of this information in the span (minus |
| 177 | // the response body which we don't want to capture anyways). |
| 178 | tracing.RunWithoutSpan(ctx, func(ctx context.Context) { |
| 179 | // We should not log at level ERROR for 5xx status codes because 5xx |
| 180 | // includes proxy errors etc. It also causes slogtest to fail |
| 181 | // instantly without an error message by default. |
| 182 | if status >= http.StatusInternalServerError { |
| 183 | logger.Warn(ctx, c.message) |
| 184 | } else { |
| 185 | logger.Debug(ctx, c.message) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | type logContextKey struct{} |
| 191 |
nothing calls this directly
no test coverage detected