Allow LogLevel to be configurable. Also add filename and line number when LogLevel>=debug

This commit is contained in:
Deluan 2020-01-13 18:47:35 -05:00
parent c995766c45
commit 3d76144f82
4 changed files with 50 additions and 25 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/cloudsonic/sonic-server/log"
"github.com/koding/multiconfig" "github.com/koding/multiconfig"
) )
@ -24,6 +25,7 @@ type sonic struct {
PlsIgnoredPatterns string `default:"^iCloud;\\~"` PlsIgnoredPatterns string `default:"^iCloud;\\~"`
// DevFlags // DevFlags
LogLevel string `default:"info"`
DevDisableAuthentication bool `default:"false"` DevDisableAuthentication bool `default:"false"`
DevDisableFileCheck bool `default:"false"` DevDisableFileCheck bool `default:"false"`
} }
@ -55,6 +57,7 @@ func LoadFromFile(tomlFile string) {
if err != nil { if err != nil {
fmt.Printf("Error loading %s: %v\n", tomlFile, err) fmt.Printf("Error loading %s: %v\n", tomlFile, err)
} }
log.SetLogLevelString(Sonic.LogLevel)
} }
func LoadFromLocalFile() { func LoadFromLocalFile() {

View file

@ -3,8 +3,9 @@ package log
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"net/http" "net/http"
"os" "runtime"
"strings" "strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -34,6 +35,26 @@ func SetLevel(l Level) {
logrus.SetLevel(logrus.Level(l)) 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 { func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Context {
if ctx == nil { if ctx == nil {
ctx = context.Background() ctx = context.Background()
@ -111,12 +132,24 @@ func parseArgs(args []interface{}) (*logrus.Entry, string) {
kvPairs := args[1:] kvPairs := args[1:]
l = addFields(l, kvPairs) 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) { switch msg := args[0].(type) {
case error: case error:
return l, msg.Error() return l, msg.Error()
case string: case string:
return l, msg return l, msg
} }
return l, "" return l, ""
} }
@ -149,28 +182,10 @@ func extractLogger(ctx interface{}) (*logrus.Entry, error) {
} }
func createNewLogger() *logrus.Entry { func createNewLogger() *logrus.Entry {
//logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true})
//l.Formatter = &logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true} //l.Formatter = &logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true}
defaultLogger.Level = logrus.Level(currentLevel) defaultLogger.Level = logrus.Level(currentLevel)
logger := logrus.NewEntry(defaultLogger) logger := logrus.NewEntry(defaultLogger)
logger.Level = logrus.Level(currentLevel) logger.Level = logrus.Level(currentLevel)
return logger 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)
}

View file

@ -60,7 +60,8 @@ func WithTx(block func(orm.Ormer) error) error {
} }
func initORM(dbPath string) error { func initORM(dbPath string) error {
orm.Debug = true verbose := conf.Sonic.LogLevel == "debug"
orm.Debug = verbose
orm.RegisterModel(new(Artist)) orm.RegisterModel(new(Artist))
orm.RegisterModel(new(Album)) orm.RegisterModel(new(Album))
orm.RegisterModel(new(MediaFile)) orm.RegisterModel(new(MediaFile))
@ -73,5 +74,5 @@ func initORM(dbPath string) error {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return orm.RunSyncdb("default", false, true) return orm.RunSyncdb("default", false, verbose)
} }

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log" "github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/scanner" "github.com/cloudsonic/sonic-server/scanner"
"github.com/go-chi/chi" "github.com/go-chi/chi"
@ -38,7 +39,7 @@ func (a *Server) MountRouter(path string, subRouter http.Handler) {
} }
func (a *Server) Run(addr string) { 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)) log.Error(http.ListenAndServe(addr, a.router))
} }
@ -68,9 +69,14 @@ func (a *Server) initImporter() {
} }
func (a *Server) startPeriodicScans() { func (a *Server) startPeriodicScans() {
first := true
for { for {
select { select {
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
if first {
log.Info("Started iTunes scanner", "xml", conf.Sonic.MusicFolder)
first = false
}
a.Importer.CheckForUpdates(false) a.Importer.CheckForUpdates(false)
} }
} }