Skip to content

Commit

Permalink
Merge pull request #50 from 2manymws/more-log
Browse files Browse the repository at this point in the history
Log more info
  • Loading branch information
k1LoW authored Jan 11, 2024
2 parents 92716cd + b3711a1 commit fa24111
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
if err != nil {
switch {
case errors.Is(err, ErrCacheNotFound):
m.logger.Debug("cache not found", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Debug("cache not found", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()))
case errors.Is(err, ErrCacheExpired):
m.logger.Warn("cache expired", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Warn("cache expired", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()))
case errors.Is(err, ErrShouldNotUseCache):
m.logger.Debug("should not use cache", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Debug("should not use cache", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()))
default:
m.logger.Error("failed to load cache", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to load cache", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()))
}
}
cacheUsed, res, err := m.cacher.Handle(req, cachedReq, cachedRes, HandlerToRequester(next), now) //nostyle:handlerrors
if err != nil {
m.logger.Error("failed to handle cache", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to handle cache", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()))
}

// Response
Expand All @@ -110,33 +110,33 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
w.WriteHeader(res.StatusCode)
body, err := io.ReadAll(res.Body)
if err != nil {
m.logger.Error("failed to read response body", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to read response body", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
} else {
if _, err := w.Write(body); err != nil {
m.logger.Error("failed to write response body", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to write response body", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
}
}
if err := res.Body.Close(); err != nil {
m.logger.Error("failed to close response body", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to close response body", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
}

if cacheUsed {
m.logger.Debug("cache used", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Debug("cache used", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
return
}
ok, expires := m.cacher.Storable(req, res, now)
if !ok {
m.logger.Debug("cache not storable", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Debug("cache not storable", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
return
}
// Restore response body
res.Body = io.NopCloser(bytes.NewReader(body))

// Store response as cache
if err := m.cacher.Store(req, res, expires); err != nil {
m.logger.Error("failed to store cache", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Error("failed to store cache", err, slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
}
m.logger.Debug("cache stored", slog.String("method", req.Method), slog.String("url", req.URL.String()))
m.logger.Debug("cache stored", slog.String("method", req.Method), slog.String("host", req.Host), slog.String("url", req.URL.String()), slog.Int("status", res.StatusCode))
})
}

Expand Down

0 comments on commit fa24111

Please sign in to comment.