From 3d76144f82227fe286e56056289a5e3e60214895 Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 13 Jan 2020 18:47:35 -0500 Subject: [PATCH] Allow LogLevel to be configurable. Also add filename and line number when LogLevel>=debug --- conf/configuration.go | 7 +++-- log/log.go | 55 +++++++++++++++++++++++++-------------- persistence/db_sql/sql.go | 5 ++-- server/app.go | 8 +++++- 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/conf/configuration.go b/conf/configuration.go index e54ff3399..5378e3db1 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/cloudsonic/sonic-server/log" "github.com/koding/multiconfig" ) @@ -24,8 +25,9 @@ type sonic struct { PlsIgnoredPatterns string `default:"^iCloud;\\~"` // DevFlags - DevDisableAuthentication bool `default:"false"` - DevDisableFileCheck bool `default:"false"` + LogLevel string `default:"info"` + DevDisableAuthentication bool `default:"false"` + DevDisableFileCheck bool `default:"false"` } var Sonic *sonic @@ -55,6 +57,7 @@ func LoadFromFile(tomlFile string) { if err != nil { fmt.Printf("Error loading %s: %v\n", tomlFile, err) } + log.SetLogLevelString(Sonic.LogLevel) } func LoadFromLocalFile() { diff --git a/log/log.go b/log/log.go index 6d44a393d..2f9d143d3 100644 --- a/log/log.go +++ b/log/log.go @@ -3,8 +3,9 @@ package log import ( "context" "errors" + "fmt" "net/http" - "os" + "runtime" "strings" "github.com/sirupsen/logrus" @@ -34,6 +35,26 @@ func SetLevel(l Level) { logrus.SetLevel(logrus.Level(l)) } +func SetLogLevelString(l string) { + envLevel := strings.ToLower(l) + var level Level + switch envLevel { + case "critical": + level = LevelCritical + case "error": + level = LevelError + case "warn": + level = LevelWarn + case "debug": + level = LevelDebug + case "trace": + level = LevelTrace + default: + level = LevelInfo + } + SetLevel(level) +} + func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Context { if ctx == nil { ctx = context.Background() @@ -111,12 +132,24 @@ func parseArgs(args []interface{}) (*logrus.Entry, string) { kvPairs := args[1:] l = addFields(l, kvPairs) } + if currentLevel >= LevelDebug { + _, file, line, ok := runtime.Caller(2) + 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, "" } @@ -149,28 +182,10 @@ func extractLogger(ctx interface{}) (*logrus.Entry, error) { } func createNewLogger() *logrus.Entry { + //logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true}) //l.Formatter = &logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true} defaultLogger.Level = logrus.Level(currentLevel) logger := logrus.NewEntry(defaultLogger) logger.Level = logrus.Level(currentLevel) return logger } - -func init() { - //logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true}) - envLevel := strings.ToLower(os.Getenv("LOG_LEVEL")) - var level Level - switch envLevel { - case "critical": - level = LevelCritical - case "error": - level = LevelError - case "warn": - level = LevelWarn - case "debug": - level = LevelDebug - default: - level = LevelInfo - } - SetLevel(level) -} diff --git a/persistence/db_sql/sql.go b/persistence/db_sql/sql.go index bf43d48b7..70967281a 100644 --- a/persistence/db_sql/sql.go +++ b/persistence/db_sql/sql.go @@ -60,7 +60,8 @@ func WithTx(block func(orm.Ormer) error) error { } func initORM(dbPath string) error { - orm.Debug = true + verbose := conf.Sonic.LogLevel == "debug" + orm.Debug = verbose orm.RegisterModel(new(Artist)) orm.RegisterModel(new(Album)) orm.RegisterModel(new(MediaFile)) @@ -73,5 +74,5 @@ func initORM(dbPath string) error { if err != nil { panic(err) } - return orm.RunSyncdb("default", false, true) + return orm.RunSyncdb("default", false, verbose) } diff --git a/server/app.go b/server/app.go index 632d00fbc..64d10ea38 100644 --- a/server/app.go +++ b/server/app.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/cloudsonic/sonic-server/conf" "github.com/cloudsonic/sonic-server/log" "github.com/cloudsonic/sonic-server/scanner" "github.com/go-chi/chi" @@ -38,7 +39,7 @@ func (a *Server) MountRouter(path string, subRouter http.Handler) { } func (a *Server) Run(addr string) { - log.Info("Starting CloudSonic server", "address", addr) + log.Info("CloudSonic server is ready to handle requests", "address", addr) log.Error(http.ListenAndServe(addr, a.router)) } @@ -68,9 +69,14 @@ func (a *Server) initImporter() { } func (a *Server) startPeriodicScans() { + first := true for { select { case <-time.After(5 * time.Second): + if first { + log.Info("Started iTunes scanner", "xml", conf.Sonic.MusicFolder) + first = false + } a.Importer.CheckForUpdates(false) } }