use logging settings in logger

This commit is contained in:
Jakob Schrettenbrunner 2017-06-27 12:05:50 +02:00
parent fbfeed4ee7
commit 02492cb64d
2 changed files with 42 additions and 6 deletions

View File

@ -28,9 +28,9 @@
"failLimit": 5 "failLimit": 5
}, },
"remote": "https://pterodactyl.app", "remote": "https://pterodactyl.app",
"logger": { "log": {
"path": "logs/", "path": "logs/",
"level": "debug", "level": "info",
"deleteAfterDays": 100 "deleteAfterDays": 100
} }
} }

View File

@ -6,6 +6,8 @@ import (
rotatelogs "github.com/lestrrat/go-file-rotatelogs" rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook" "github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/schrej/wings/config"
) )
// ConfigureLogging configures logrus to our needs // ConfigureLogging configures logrus to our needs
@ -15,20 +17,54 @@ func ConfigureLogging() error {
DisableTimestamp: true, DisableTimestamp: true,
}) })
path := "logs/" path := config.Get().Log.Path
writer := rotatelogs.New( writer := rotatelogs.New(
path+"pterodactyld.%Y%m%d-%H%M.log", path+"wings.%Y%m%d-%H%M.log",
rotatelogs.WithLinkName(path), rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second), rotatelogs.WithMaxAge(time.Duration(config.Get().Log.DeleteAfterDays)*time.Hour*24),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second), rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
) )
level := config.Get().Log.Level
// In debug mode the log level is always debug
if config.Get().Debug {
level = "debug"
}
log.SetLevel(log.DebugLevel)
// Apply log level
switch level {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
}
log.AddHook(lfshook.NewHook(lfshook.WriterMap{ log.AddHook(lfshook.NewHook(lfshook.WriterMap{
log.DebugLevel: writer,
log.InfoLevel: writer, log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer, log.ErrorLevel: writer,
log.FatalLevel: writer, log.FatalLevel: writer,
log.PanicLevel: writer,
})) }))
log.Info("Log level: " + level)
log.Debug("Test")
return nil return nil
} }