2017-10-09 19:01:24 +00:00
|
|
|
package utils
|
2017-06-18 22:01:44 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-16 11:56:20 +00:00
|
|
|
"os"
|
2017-09-16 12:00:11 +00:00
|
|
|
"path/filepath"
|
2017-06-18 22:01:44 +00:00
|
|
|
"time"
|
|
|
|
|
2018-02-20 20:25:31 +00:00
|
|
|
"github.com/pterodactyl/wings/constants"
|
2017-09-16 11:56:20 +00:00
|
|
|
|
2018-02-20 22:38:29 +00:00
|
|
|
"github.com/lestrrat/go-file-rotatelogs"
|
2017-06-18 22:01:44 +00:00
|
|
|
"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
|
|
|
|
2018-02-20 20:25:31 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2017-06-18 22:01:44 +00:00
|
|
|
)
|
|
|
|
|
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() {
|
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)
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-09-16 12:00:11 +00:00
|
|
|
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
|
|
|
|
}
|
2017-06-18 22:01:44 +00:00
|
|
|
writer := rotatelogs.New(
|
2017-09-16 12:00:11 +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
|
|
|
}
|