Skip to content

Commit

Permalink
Reduce repetition when logging
Browse files Browse the repository at this point in the history
  • Loading branch information
popcorn committed Jun 28, 2024
1 parent 4595b8a commit 17db5bf
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,21 @@ func (h *Handler) rateLimitExceeded(w http.ResponseWriter, r *http.Request, repl
remoteIP = r.RemoteAddr // assume there was no port, I guess
}

// Create logger with common fields
logger := h.logger.With(
zap.String("zone", zoneName),
zap.Duration("wait", wait),
zap.String("remote_ip", remoteIP),
)

// Conditionally add the key field
if h.LogKey {
h.logger.Info("rate limit exceeded",
zap.String("zone", zoneName),
zap.String("key", key),
zap.Duration("wait", wait),
zap.String("remote_ip", remoteIP),
)
} else {
h.logger.Info("rate limit exceeded",
zap.String("zone", zoneName),
zap.Duration("wait", wait),
zap.String("remote_ip", remoteIP),
)
logger = logger.With(zap.String("key", key))
}

// Log the rate limit exceeded message
logger.Info("rate limit exceeded")

// make some information about this rate limit available
repl.Set("http.rate_limit.exceeded.name", zoneName)

Expand Down

0 comments on commit 17db5bf

Please sign in to comment.