Skip to content

Commit 9a725fe

Browse files
committed
feat: add better logging
1 parent be54b7d commit 9a725fe

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pkg/http/cache.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (h handler) asyncCacheRevalidate(key string, orig *http.Request) func() {
227227
req.Header = cloneHeaders(orig.Header)
228228
stripHopByHop(req.Header)
229229
req.Header.Del("Range")
230-
log.Ctx(orig.Context()).Debug().Str("key", key).Str("url", u.String()).Msg("revalidating cache")
230+
log.Ctx(orig.Context()).Debug().Str("cachKey", key).Msg("revalidating cache")
231231

232232
resp, err := h.httpClient.Do(req)
233233
if err != nil {
@@ -243,7 +243,13 @@ func (h handler) asyncCacheRevalidate(key string, orig *http.Request) func() {
243243
}
244244

245245
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
246-
logger := log.With().Str("path", r.URL.Path).Str("method", r.Method).Logger()
246+
logger := log.With().Fields(map[string]any{
247+
"request": map[string]any{
248+
"path": r.URL.Path,
249+
"method": strings.ToLower(r.Method),
250+
"queryParams": r.URL.Query().Encode(),
251+
},
252+
}).Logger()
247253
ctx := logger.WithContext(r.Context())
248254

249255
if strings.ToUpper(r.Method) != http.MethodGet || r.Header.Get("Range") != "" {
@@ -275,7 +281,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
275281
if entry.IsStale() {
276282
go h.worker.Start(key, h.asyncCacheRevalidate(key, r))
277283
}
278-
logger.Debug().Str("key", key).Int("status", entry.Status).Msg("serving cached response")
284+
logger.Debug().Str("cacheKey", key).Msg("serving cached response")
279285
serveResponseFromMemory(w, *entry)
280286
}
281287

@@ -288,6 +294,7 @@ func (h handler) cacheResponse(ctx context.Context, key string) func(*http.Respo
288294
return nil
289295
}
290296
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent) {
297+
lg.Debug().Str("cacheKey", key).Int("status", resp.StatusCode).Msg("skipping cache: not 200 or 204")
291298
return nil
292299
}
293300
// TODO: add cache control back in
@@ -299,7 +306,7 @@ func (h handler) cacheResponse(ctx context.Context, key string) func(*http.Respo
299306
return nil
300307
}
301308

302-
lg.Debug().Str("key", key).Int("status", resp.StatusCode).Msg("caching response")
309+
lg.Debug().Str("cacheKey", key).Msg("caching response")
303310

304311
lr := &io.LimitedReader{R: resp.Body, N: maxCacheBytes + 1}
305312
body, err := io.ReadAll(lr)

0 commit comments

Comments
 (0)