浏览代码

httpd request logger: set log level based on the status code

Fixes #1393

Signed-off-by: Nicola Murino <[email protected]>
Nicola Murino 2 年之前
父节点
当前提交
397cad93df
共有 1 个文件被更改,包括 9 次插入1 次删除
  1. 9 1
      internal/logger/request_logger.go

+ 9 - 1
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).