5ce6e16d96
* feat: add album refresh functionality after deleting missing files Implemented RefreshAlbums method in AlbumRepository to recalculate album attributes (size, duration, song count) from their constituent media files. This method processes albums in batches to maintain efficiency with large datasets. Added integration in deleteMissingFiles to automatically refresh affected albums in the background after deleting missing media files, ensuring album statistics remain accurate. Includes comprehensive test coverage for various scenarios including single/multiple albums, empty batches, and large batch processing. Signed-off-by: Deluan <deluan@navidrome.org> * refactor: extract missing files deletion into reusable service layer Extracted inline deletion logic from server/nativeapi/missing.go into a new core.MissingFiles service interface and implementation. This provides better separation of concerns and testability. The MissingFiles service handles: - Deletion of specific or all missing files via transaction - Garbage collection after deletion - Extraction of affected album IDs from missing files - Background refresh of artist and album statistics The deleteMissingFiles HTTP handler now simply delegates to the service, removing 70+ lines of inline logic. All deletion, transaction, and stat refresh logic is now centralized in core/missing_files.go. Updated dependency injection to provide MissingFiles service to the native API router. Renamed receiver variable from 'n' to 'api' throughout native_api.go for consistency. * refactor: consolidate maintenance operations into unified service Consolidate MissingFiles and RefreshAlbums functionality into a new Maintenance service. This refactoring: - Creates core.Maintenance interface combining DeleteMissingFiles, DeleteAllMissingFiles, and RefreshAlbums methods - Moves RefreshAlbums logic from AlbumRepository persistence layer to core Maintenance service - Removes MissingFiles interface and moves its implementation to maintenanceService - Updates all references in wire providers, native API router, and handlers - Removes RefreshAlbums interface method from AlbumRepository model - Improves separation of concerns by centralizing maintenance operations in the core domain This change provides a cleaner API and better organization of maintenance-related database operations. * refactor: remove MissingFiles interface and update references Remove obsolete MissingFiles interface and its references: - Delete core/missing_files.go and core/missing_files_test.go - Remove RefreshAlbums method from AlbumRepository interface and implementation - Remove RefreshAlbums tests from AlbumRepository test suite - Update wire providers to use NewMaintenance instead of NewMissingFiles - Update native API router to use Maintenance service - Update missing.go handler to use Maintenance interface All functionality is now consolidated in the core.Maintenance service. Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename RefreshAlbums to refreshAlbums and update related calls Signed-off-by: Deluan <deluan@navidrome.org> * refactor: optimize album refresh logic and improve test coverage Signed-off-by: Deluan <deluan@navidrome.org> * refactor: simplify logging setup in tests with reusable LogHook function Signed-off-by: Deluan <deluan@navidrome.org> * refactor: add synchronization to logger and maintenance service for thread safety Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
343 lines
7.0 KiB
Go
343 lines
7.0 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 interface{}, msg interface{}, keyValuePairs ...interface{})
|
|
|
|
var redacted = &Hook{
|
|
AcceptedLevels: logrus.AllLevels,
|
|
RedactionList: []string{
|
|
// Keys from the config
|
|
"(ApiKey:\")[\\w]*",
|
|
"(Secret:\")[\\w]*",
|
|
"(Spotify.*ID:\")[\\w]*",
|
|
"(PasswordEncryptionKey:[\\s]*\")[^\"]*",
|
|
"(ReverseProxyUserHeader:[\\s]*\")[^\"]*",
|
|
"(ReverseProxyWhitelist:[\\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) {
|
|
currentLevel = l
|
|
loggerMu.Lock()
|
|
defaultLogger.Level = logrus.TraceLevel
|
|
loggerMu.Unlock()
|
|
logrus.SetLevel(logrus.Level(l))
|
|
}
|
|
|
|
func SetLevelString(l string) {
|
|
level := levelFromString(l)
|
|
SetLevel(level)
|
|
}
|
|
|
|
func levelFromString(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) {
|
|
logLevels = nil
|
|
for k, v := range levels {
|
|
logLevels = append(logLevels, levelPath{path: k, level: levelFromString(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)
|
|
}
|
|
|
|
// Redact applies redaction to a single string
|
|
func Redact(msg string) string {
|
|
r, _ := redacted.redact(msg)
|
|
return r
|
|
}
|
|
|
|
func NewContext(ctx context.Context, keyValuePairs ...interface{}) 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
|
|
}
|
|
|
|
func SetDefaultLogger(l *logrus.Logger) {
|
|
loggerMu.Lock()
|
|
defer loggerMu.Unlock()
|
|
defaultLogger = l
|
|
}
|
|
|
|
func CurrentLevel() Level {
|
|
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 ...interface{}) {
|
|
log(LevelFatal, args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func Error(args ...interface{}) {
|
|
log(LevelError, args...)
|
|
}
|
|
|
|
func Warn(args ...interface{}) {
|
|
log(LevelWarn, args...)
|
|
}
|
|
|
|
func Info(args ...interface{}) {
|
|
log(LevelInfo, args...)
|
|
}
|
|
|
|
func Debug(args ...interface{}) {
|
|
log(LevelDebug, args...)
|
|
}
|
|
|
|
func Trace(args ...interface{}) {
|
|
log(LevelTrace, args...)
|
|
}
|
|
|
|
func log(level Level, args ...interface{}) {
|
|
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 {
|
|
if currentLevel >= requiredLevel {
|
|
return true
|
|
}
|
|
if len(logLevels) == 0 {
|
|
return false
|
|
}
|
|
|
|
_, file, _, ok := runtime.Caller(skip)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
file = strings.TrimPrefix(file, rootPath)
|
|
for _, lp := range logLevels {
|
|
if strings.HasPrefix(file, lp.path) {
|
|
return lp.level >= requiredLevel
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func parseArgs(args []interface{}) (*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 []interface{}) *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 interface{}) (*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")
|
|
}
|