wings/tools/logging.go

71 lines
1.3 KiB
Go
Raw Normal View History

package tools
import (
"time"
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
2017-06-27 10:05:50 +00:00
"github.com/schrej/wings/config"
)
// ConfigureLogging configures logrus to our needs
func ConfigureLogging() error {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
2017-06-27 10:05:50 +00:00
path := config.Get().Log.Path
writer := rotatelogs.New(
2017-06-27 10:05:50 +00:00
path+"wings.%Y%m%d-%H%M.log",
rotatelogs.WithLinkName(path),
2017-06-27 10:05:50 +00:00
rotatelogs.WithMaxAge(time.Duration(config.Get().Log.DeleteAfterDays)*time.Hour*24),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)
2017-06-27 10:05:50 +00:00
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{
2017-06-27 10:05:50 +00:00
log.DebugLevel: writer,
log.InfoLevel: writer,
2017-06-27 10:05:50 +00:00
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
}))
2017-06-27 10:05:50 +00:00
log.Info("Log level: " + level)
log.Debug("Test")
return nil
}