From 397cad93dff57fbae59b08e6e46fac4ea60d9b94 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 20 Aug 2023 19:01:16 +0200 Subject: [PATCH] httpd request logger: set log level based on the status code Fixes #1393 Signed-off-by: Nicola Murino --- internal/logger/request_logger.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/logger/request_logger.go b/internal/logger/request_logger.go index 752a3e38..5dee5a7b 100644 --- a/internal/logger/request_logger.go +++ b/internal/logger/request_logger.go @@ -73,7 +73,15 @@ func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry { // Write logs a new entry at the end of the HTTP request func (l *StructuredLoggerEntry) Write(status, bytes int, _ http.Header, elapsed time.Duration, _ any) { metric.HTTPRequestServed(status) - l.Logger.Info(). + var ev *zerolog.Event + if status >= http.StatusInternalServerError { + ev = l.Logger.Error() + } else if status >= http.StatusBadRequest { + ev = l.Logger.Warn() + } else { + ev = l.Logger.Debug() + } + ev. Timestamp(). Str("sender", "httpd"). Fields(l.fields).