diff --git a/log/log.go b/log/log.go index 3ffbb14b9..32462bf5e 100644 --- a/log/log.go +++ b/log/log.go @@ -109,6 +109,7 @@ func levelFromString(l string) Level { // SetLogLevels sets the log levels for specific paths in the codebase. func SetLogLevels(levels map[string]string) { + logLevels = nil for k, v := range levels { logLevels = append(logLevels, levelPath{path: k, level: levelFromString(v)}) } @@ -158,7 +159,7 @@ func CurrentLevel() Level { // IsGreaterOrEqualTo returns true if the caller's current log level is equal or greater than the provided level. func IsGreaterOrEqualTo(level Level) bool { - return shouldLog(level) + return shouldLog(level, 2) } func Fatal(args ...interface{}) { @@ -187,14 +188,14 @@ func Trace(args ...interface{}) { } func log(level Level, args ...interface{}) { - if !shouldLog(level) { + if !shouldLog(level, 3) { return } logger, msg := parseArgs(args) logger.Log(logrus.Level(level), msg) } -func shouldLog(requiredLevel Level) bool { +func shouldLog(requiredLevel Level, skip int) bool { if currentLevel >= requiredLevel { return true } @@ -202,7 +203,7 @@ func shouldLog(requiredLevel Level) bool { return false } - _, file, _, ok := runtime.Caller(3) + _, file, _, ok := runtime.Caller(skip) if !ok { return false } diff --git a/log/log_test.go b/log/log_test.go index 0462988ef..a1f3b6ba0 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -150,6 +150,10 @@ var _ = Describe("Logger", func() { }) Describe("IsGreaterOrEqualTo", func() { + BeforeEach(func() { + SetLogLevels(nil) + }) + It("returns false if log level is below provided level", func() { SetLevel(LevelError) Expect(IsGreaterOrEqualTo(LevelWarn)).To(BeFalse()) diff --git a/server/middlewares.go b/server/middlewares.go index 62e5bb51e..7843b1676 100644 --- a/server/middlewares.go +++ b/server/middlewares.go @@ -3,6 +3,7 @@ package server import ( "cmp" "context" + "encoding/json" "errors" "fmt" "io/fs" @@ -42,7 +43,10 @@ func requestLogger(next http.Handler) http.Handler { "httpStatus", ww.Status(), "responseSize", ww.BytesWritten(), } - if log.IsGreaterOrEqualTo(log.LevelDebug) { + if log.IsGreaterOrEqualTo(log.LevelTrace) { + headers, _ := json.Marshal(r.Header) + logArgs = append(logArgs, "header", string(headers)) + } else if log.IsGreaterOrEqualTo(log.LevelDebug) { logArgs = append(logArgs, "userAgent", r.UserAgent()) }