2017-06-18 22:01:44 +00:00
|
|
|
package tools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
|
|
|
|
"github.com/rifflock/lfshook"
|
2017-06-26 18:44:37 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-31 18:13:01 +00:00
|
|
|
"github.com/spf13/viper"
|
2017-06-27 10:05:50 +00:00
|
|
|
|
2017-07-01 17:26:05 +00:00
|
|
|
"github.com/Pterodactyl/wings/config"
|
2017-06-18 22:01:44 +00:00
|
|
|
)
|
|
|
|
|
2017-07-06 18:52:08 +00:00
|
|
|
func InitLogging() {
|
2017-06-18 22:01:44 +00:00
|
|
|
log.SetFormatter(&log.TextFormatter{
|
|
|
|
DisableTimestamp: true,
|
|
|
|
})
|
|
|
|
|
2017-07-06 18:52:08 +00:00
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigureLogging configures logrus to our needs
|
|
|
|
func ConfigureLogging() error {
|
|
|
|
|
2017-08-31 18:13:01 +00:00
|
|
|
path := viper.GetString(config.LogPath)
|
2017-06-18 22:01:44 +00:00
|
|
|
writer := rotatelogs.New(
|
2017-06-27 10:05:50 +00:00
|
|
|
path+"wings.%Y%m%d-%H%M.log",
|
2017-06-18 22:01:44 +00:00
|
|
|
rotatelogs.WithLinkName(path),
|
2017-08-31 18:13:01 +00:00
|
|
|
rotatelogs.WithMaxAge(time.Duration(viper.GetInt(config.LogDeleteAfterDays))*time.Hour*24),
|
2017-06-18 22:01:44 +00:00
|
|
|
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
|
|
|
|
)
|
|
|
|
|
2017-07-06 18:52:08 +00:00
|
|
|
log.AddHook(lfshook.NewHook(lfshook.WriterMap{
|
|
|
|
log.DebugLevel: writer,
|
|
|
|
log.InfoLevel: writer,
|
|
|
|
log.WarnLevel: writer,
|
|
|
|
log.ErrorLevel: writer,
|
|
|
|
log.FatalLevel: writer,
|
|
|
|
}))
|
|
|
|
|
2017-08-31 18:13:01 +00:00
|
|
|
level := viper.GetString(config.LogLevel)
|
2017-06-27 10:05:50 +00:00
|
|
|
|
|
|
|
// In debug mode the log level is always debug
|
2017-08-31 18:13:01 +00:00
|
|
|
if viper.GetBool(config.Debug) {
|
2017-06-27 10:05:50 +00:00
|
|
|
level = "debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.Info("Log level: " + level)
|
|
|
|
|
2017-06-26 18:44:37 +00:00
|
|
|
return nil
|
2017-06-18 22:01:44 +00:00
|
|
|
}
|