wings/utils/logger.go

84 lines
1.7 KiB
Go
Raw Normal View History

2017-10-09 19:01:24 +00:00
package utils
import (
2017-09-16 11:56:20 +00:00
"os"
"path/filepath"
"time"
//"time"
"github.com/pterodactyl/wings/constants"
2017-09-16 11:56:20 +00:00
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
2017-06-27 10:05:50 +00:00
"github.com/pterodactyl/wings/config"
)
2017-09-16 11:56:20 +00:00
// InitLogging initalizes the logging library for first use.
2017-07-06 18:52:08 +00:00
func InitLogging() {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
2017-07-06 18:52:08 +00:00
log.SetLevel(log.InfoLevel)
}
2017-09-16 11:56:20 +00:00
// ConfigureLogging applies the configuration to the logging library.
2017-07-06 18:52:08 +00:00
func ConfigureLogging() error {
path := filepath.Clean(viper.GetString(config.LogPath))
2017-09-24 17:47:40 +00:00
if err := os.MkdirAll(path, constants.DefaultFolderPerms); err != nil {
2017-09-16 11:56:20 +00:00
return err
}
writer, err := rotatelogs.New(
path+"/wings.%Y%m%d-%H%M.log",
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(viper.GetInt(config.LogDeleteAfterDays))*time.Hour*24),
rotatelogs.WithRotationTime(time.Hour*24),
)
if err != nil {
return err
}
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,
}, &log.JSONFormatter{}))
2017-07-06 18:52:08 +00:00
level := viper.GetString(config.LogLevel)
2017-06-27 10:05:50 +00:00
// In debug mode the log level is always debug
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)
return nil
}