mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-01 19:47:37 +03:00
Add http headers to trace log
This commit is contained in:
parent
88eac6d7f3
commit
4044642abf
3 changed files with 14 additions and 5 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue