package common import ( "encoding/json" "fmt" "io" "os" "github.com/sirupsen/logrus" ) var Log = logrus.New() func Debug(message string) { Log.Debug(message) } func Info(message string) { Log.Info(message) } func Error(message string) { Log.Error(message) } func Warn(message string) { Log.Warn(message) } func InitLogging() { logLevel, err := logrus.ParseLevel(Config.Application.LogLevel) if err != nil { panic(fmt.Sprintf("Invalid configured logLevel: %s\n", Config.Application.LogLevel)) } Log.SetLevel(logLevel) Log.SetFormatter(&logrus.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", PadLevelText: true, DisableQuote: true, }) LogFile := Config.Application.LogFile file, err := os.OpenFile( LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0655, ) if err != nil { msg := fmt.Sprintf("Failed to log to file %s: %s", Config.Application.LogFile, err) Log.Warning(msg) panic(msg) } mw := io.MultiWriter(os.Stdout, file) Log.SetOutput(mw) configJson, err := json.Marshal(Config) if err == nil { Info(fmt.Sprintf("Using config: %s", configJson)) } }