From 17db5bfd148e9d33bb31373ef5a8e305daa06eb3 Mon Sep 17 00:00:00 2001 From: Drago Crnjac Date: Fri, 28 Jun 2024 18:03:41 +0100 Subject: [PATCH] Reduce repetition when logging --- handler.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/handler.go b/handler.go index aa73be2..6441506 100644 --- a/handler.go +++ b/handler.go @@ -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)