From 05fa0cd80243d534dc3ad92f2d2e666c2a2dc12f Mon Sep 17 00:00:00 2001 From: Aleksander Piotrowski Date: Fri, 24 Jan 2025 16:51:43 +0100 Subject: [PATCH] Add more logger improvements (#34) --- zap/middleware.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/zap/middleware.go b/zap/middleware.go index f78d6c6..5e775b2 100644 --- a/zap/middleware.go +++ b/zap/middleware.go @@ -53,17 +53,30 @@ func (w *Logger) EchoMiddleware() echo.MiddlewareFunc { log := w.Ctx(c.Request().Context()).With(fields...) n := res.Status + + if err != nil { + log = log.Err(err) + } + + // Do not log the error when request canceled by the client + if errors.Is(err, context.Canceled) { + log.Info("Request canceled") + + return nil + } + // Status 5XX is logged as error as this should not happen in production. if n >= 500 { - // Do not log the error when request canceled by the client - if errors.Is(err, context.Canceled) { - log.Err(err).Info("Request canceled") - } else { - log.Err(err).Error("Error") - } - } else { - log.Info("Incoming request") + log.Error("Error") + + return nil + } + if n >= 400 { + log.Warn("Warn") + + return nil } + log.Info("Info") return nil }