mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-01 07:21:17 +00:00
* fix(scanner): stop logging expected lyrics sniff misses as warnings During a scan, embedded lyrics are parsed with an empty suffix, which puts ParseLyrics into content-sniffing mode: it tries the TTML, SRT and Lyricsfile YAML parsers in turn before falling back to plain text. Every plain-text or LRC lyric therefore fails the structured probes on its way to the fallback, and each failure was logged at warning level with no indication of which file triggered it, flooding the scan log with benign "Error parsing lyrics, falling back to plain text" messages. A probe rejecting content it does not own during sniffing is expected control flow, so it is now logged at trace instead. A parse failure under an explicitly requested suffix (e.g. a malformed .yaml/.srt/.ttml sidecar) still warns, since the user declared that format. ParseLyrics gains ctx and path parameters so any warning names the offending file and carries request context where available; all call sites are updated accordingly. Also fixes a test-isolation bug in the new logging spec: the BeforeEach swapped the process-global default logger via SetDefaultLogger but only restored the log level on cleanup, leaking the null logger and its hook into later specs in the shared model suite. * test: use spec-scoped contexts instead of context.Background in lyrics tests Replace context.Background() with GinkgoT().Context() (and b.Context() in the parse benchmarks) across the lyrics-related tests, so contexts are cancelled when each spec ends. The embeddedLyrics fixture in core/lyrics is now a hand-written literal like its sibling fixtures, removing the construction-time ParseLyrics call that could not use a spec-scoped context. * refactor(model): attach lyrics parse log attribution via context Narrow ParseLyrics back to (ctx, suffix, lang, contents), dropping the path parameter added by the previous commit. Attribution now uses the codebase's existing idiom: callers that know the source attach it with log.NewContext (e.g. "file" for the media file or sidecar), and the plugin adapter tags both the plugin name and the track, fixing probe-miss logs that misattributed plugin-returned content to the file's own tags. This removes three adjacent string parameters that were easy to swap silently, and the "" placeholder most call sites had to pass. Also hardens the logging spec from the previous commit: the null test logger is now swapped in before raising the level (SetLevel forces the current default logger to trace, so the old order left the null logger at info and trace entries never reached the hook), the sniff test now asserts probe misses are observable at trace with file attribution instead of only asserting the absence of warnings, and cleanup restores the actual previous logger — via a new return value on log.SetDefaultLogger — instead of a bare logrus.New() that would discard hooks configured on the process-wide logger. * refactor(lyrics): hoist attributed log contexts out of loops Address review feedback on #5702: build the log-attributed context once per operation instead of per iteration, and reuse it on the surrounding log calls so the error/trace lines around ParseLyrics carry the same attribution fields. In fromExternalFile the sidecar path now rides the context for all log lines in the function, replacing the repeated explicit "path" field. * style(model): pass lyrics parse errors as final log arguments Per the project logging convention, errors go as the last argument (the log package normalizes them via its error case) instead of a keyed "error" pair, which stores the raw error value and bypasses that handling. Flagged by review on #5702; the keyed form was inherited from the original warning line.
369 lines
7.6 KiB
Go
369 lines
7.6 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"iter"
|
|
"net/http"
|
|
"os"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Level uint32
|
|
|
|
type LevelFunc = func(ctx any, msg any, keyValuePairs ...any)
|
|
|
|
var redacted = &Hook{
|
|
AcceptedLevels: logrus.AllLevels,
|
|
RedactionList: []string{
|
|
// Keys from the config
|
|
"(ApiKey:\")[\\w]*",
|
|
"(Secret:\")[\\w]*",
|
|
"(PasswordEncryptionKey:[\\s]*\")[^\"]*",
|
|
"(UserHeader:[\\s]*\")[^\"]*",
|
|
"(TrustedSources:[\\s]*\")[^\"]*",
|
|
"(MetricsPath:[\\s]*\")[^\"]*",
|
|
"(DevAutoCreateAdminPassword:[\\s]*\")[^\"]*",
|
|
"(DevAutoLoginUsername:[\\s]*\")[^\"]*",
|
|
|
|
// UI appConfig
|
|
"(subsonicToken:)[\\w]+(\\s)",
|
|
"(subsonicSalt:)[\\w]+(\\s)",
|
|
"(token:)[^\\s]+",
|
|
|
|
// Subsonic query params
|
|
"([^\\w]t=)[\\w]+",
|
|
"([^\\w]s=)[^&]+",
|
|
"([^\\w]p=)[^&]+",
|
|
"([^\\w]jwt=)[^&]+",
|
|
|
|
// External services query params
|
|
"([^\\w]api_key=)[\\w]+",
|
|
},
|
|
}
|
|
|
|
const (
|
|
LevelFatal = Level(logrus.FatalLevel)
|
|
LevelError = Level(logrus.ErrorLevel)
|
|
LevelWarn = Level(logrus.WarnLevel)
|
|
LevelInfo = Level(logrus.InfoLevel)
|
|
LevelDebug = Level(logrus.DebugLevel)
|
|
LevelTrace = Level(logrus.TraceLevel)
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const loggerCtxKey = contextKey("logger")
|
|
|
|
type levelPath struct {
|
|
path string
|
|
level Level
|
|
}
|
|
|
|
var (
|
|
currentLevel Level
|
|
loggerMu sync.RWMutex
|
|
defaultLogger = logrus.New()
|
|
logSourceLine = false
|
|
rootPath string
|
|
logLevels []levelPath
|
|
)
|
|
|
|
// SetLevel sets the global log level used by the simple logger.
|
|
func SetLevel(l Level) {
|
|
loggerMu.Lock()
|
|
currentLevel = l
|
|
defaultLogger.Level = logrus.TraceLevel
|
|
loggerMu.Unlock()
|
|
logrus.SetLevel(logrus.Level(l))
|
|
}
|
|
|
|
func SetLevelString(l string) {
|
|
level := ParseLogLevel(l)
|
|
SetLevel(level)
|
|
}
|
|
|
|
func ParseLogLevel(l string) Level {
|
|
envLevel := strings.ToLower(l)
|
|
var level Level
|
|
switch envLevel {
|
|
case "fatal":
|
|
level = LevelFatal
|
|
case "error":
|
|
level = LevelError
|
|
case "warn":
|
|
level = LevelWarn
|
|
case "debug":
|
|
level = LevelDebug
|
|
case "trace":
|
|
level = LevelTrace
|
|
default:
|
|
level = LevelInfo
|
|
}
|
|
return level
|
|
}
|
|
|
|
// SetLogLevels sets the log levels for specific paths in the codebase.
|
|
func SetLogLevels(levels map[string]string) {
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
logLevels = nil
|
|
for k, v := range levels {
|
|
logLevels = append(logLevels, levelPath{path: k, level: ParseLogLevel(v)})
|
|
}
|
|
sort.Slice(logLevels, func(i, j int) bool {
|
|
return logLevels[i].path > logLevels[j].path
|
|
})
|
|
}
|
|
|
|
func SetLogSourceLine(enabled bool) {
|
|
logSourceLine = enabled
|
|
}
|
|
|
|
func SetRedacting(enabled bool) {
|
|
if enabled {
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
defaultLogger.AddHook(redacted)
|
|
}
|
|
}
|
|
|
|
func SetOutput(w io.Writer) {
|
|
if runtime.GOOS == "windows" {
|
|
w = CRLFWriter(w)
|
|
}
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
defaultLogger.SetOutput(w)
|
|
}
|
|
|
|
// EnableJournalFormat wraps the current logger formatter with syslog
|
|
// priority prefixes for systemd-journald. Only call this when output
|
|
// goes to stderr and JOURNAL_STREAM is set.
|
|
func EnableJournalFormat() {
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
defaultLogger.Formatter = &journalFormatter{inner: defaultLogger.Formatter}
|
|
}
|
|
|
|
// Redact applies redaction to a single string
|
|
func Redact(msg string) string {
|
|
r, _ := redacted.redact(msg)
|
|
return r
|
|
}
|
|
|
|
func NewContext(ctx context.Context, keyValuePairs ...any) context.Context {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
logger, ok := ctx.Value(loggerCtxKey).(*logrus.Entry)
|
|
if !ok {
|
|
logger = createNewLogger()
|
|
}
|
|
logger = addFields(logger, keyValuePairs)
|
|
ctx = context.WithValue(ctx, loggerCtxKey, logger)
|
|
|
|
return ctx
|
|
}
|
|
|
|
// SetDefaultLogger swaps the process-wide logger and returns the previous one,
|
|
// so tests can restore the original (with its hooks and formatter) on cleanup.
|
|
func SetDefaultLogger(l *logrus.Logger) *logrus.Logger {
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
prev := defaultLogger
|
|
defaultLogger = l
|
|
return prev
|
|
}
|
|
|
|
func CurrentLevel() Level {
|
|
loggerMu.RLock()
|
|
defer loggerMu.RUnlock()
|
|
return currentLevel
|
|
}
|
|
|
|
// 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, 2)
|
|
}
|
|
|
|
func Fatal(args ...any) {
|
|
log(LevelFatal, args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func Error(args ...any) {
|
|
log(LevelError, args...)
|
|
}
|
|
|
|
func Warn(args ...any) {
|
|
log(LevelWarn, args...)
|
|
}
|
|
|
|
func Info(args ...any) {
|
|
log(LevelInfo, args...)
|
|
}
|
|
|
|
func Debug(args ...any) {
|
|
log(LevelDebug, args...)
|
|
}
|
|
|
|
func Trace(args ...any) {
|
|
log(LevelTrace, args...)
|
|
}
|
|
|
|
func Log(level Level, args ...any) {
|
|
log(level, args...)
|
|
}
|
|
|
|
func log(level Level, args ...any) {
|
|
if !shouldLog(level, 3) {
|
|
return
|
|
}
|
|
|
|
logger, msg := parseArgs(args)
|
|
logger.Log(logrus.Level(level), msg)
|
|
}
|
|
|
|
func Writer() io.Writer {
|
|
loggerMu.RLock()
|
|
defer loggerMu.RUnlock()
|
|
return defaultLogger.Writer()
|
|
}
|
|
|
|
func shouldLog(requiredLevel Level, skip int) bool {
|
|
loggerMu.RLock()
|
|
level := currentLevel
|
|
levels := logLevels
|
|
loggerMu.RUnlock()
|
|
|
|
if level >= requiredLevel {
|
|
return true
|
|
}
|
|
if len(levels) == 0 {
|
|
return false
|
|
}
|
|
|
|
_, file, _, ok := runtime.Caller(skip)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
file = strings.TrimPrefix(file, rootPath)
|
|
for _, lp := range levels {
|
|
if strings.HasPrefix(file, lp.path) {
|
|
return lp.level >= requiredLevel
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func parseArgs(args []any) (*logrus.Entry, string) {
|
|
var l *logrus.Entry
|
|
var err error
|
|
if args[0] == nil {
|
|
l = createNewLogger()
|
|
args = args[1:]
|
|
} else {
|
|
l, err = extractLogger(args[0])
|
|
if err != nil {
|
|
l = createNewLogger()
|
|
} else {
|
|
args = args[1:]
|
|
}
|
|
}
|
|
if len(args) > 1 {
|
|
kvPairs := args[1:]
|
|
l = addFields(l, kvPairs)
|
|
}
|
|
if logSourceLine {
|
|
_, file, line, ok := runtime.Caller(3)
|
|
if !ok {
|
|
file = "???"
|
|
line = 0
|
|
}
|
|
//_, filename := path.Split(file)
|
|
//l = l.WithField("filename", filename).WithField("line", line)
|
|
l = l.WithField(" source", fmt.Sprintf("file://%s:%d", file, line))
|
|
}
|
|
|
|
switch msg := args[0].(type) {
|
|
case error:
|
|
return l, msg.Error()
|
|
case string:
|
|
return l, msg
|
|
}
|
|
|
|
return l, ""
|
|
}
|
|
|
|
func addFields(logger *logrus.Entry, keyValuePairs []any) *logrus.Entry {
|
|
for i := 0; i < len(keyValuePairs); i += 2 {
|
|
switch name := keyValuePairs[i].(type) {
|
|
case error:
|
|
logger = logger.WithField("error", name.Error())
|
|
case string:
|
|
if i+1 >= len(keyValuePairs) {
|
|
logger = logger.WithField(name, "!!!!Invalid number of arguments in log call!!!!")
|
|
} else {
|
|
switch v := keyValuePairs[i+1].(type) {
|
|
case time.Duration:
|
|
logger = logger.WithField(name, ShortDur(v))
|
|
case fmt.Stringer:
|
|
logger = logger.WithField(name, StringerValue(v))
|
|
case iter.Seq[string]:
|
|
logger = logger.WithField(name, formatSeq(v))
|
|
case []string:
|
|
logger = logger.WithField(name, formatSlice(v))
|
|
default:
|
|
logger = logger.WithField(name, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return logger
|
|
}
|
|
|
|
func extractLogger(ctx any) (*logrus.Entry, error) {
|
|
switch ctx := ctx.(type) {
|
|
case *logrus.Entry:
|
|
return ctx, nil
|
|
case context.Context:
|
|
logger := ctx.Value(loggerCtxKey)
|
|
if logger != nil {
|
|
return logger.(*logrus.Entry), nil
|
|
}
|
|
return extractLogger(NewContext(ctx))
|
|
case *http.Request:
|
|
return extractLogger(ctx.Context())
|
|
}
|
|
return nil, errors.New("no logger found")
|
|
}
|
|
|
|
func createNewLogger() *logrus.Entry {
|
|
//logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true})
|
|
//l.Formatter = &logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true}
|
|
loggerMu.RLock()
|
|
defer loggerMu.RUnlock()
|
|
logger := logrus.NewEntry(defaultLogger)
|
|
return logger
|
|
}
|
|
|
|
func init() {
|
|
defaultLogger.Level = logrus.TraceLevel
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
return
|
|
}
|
|
rootPath = strings.TrimSuffix(file, "log/log.go")
|
|
}
|