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"
"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() {

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}
}