sirius/cmd/apisirius/logger.go

47 lines
993 B
Go
Raw Normal View History

2023-04-25 13:14:01 +03:00
package main
import (
"io"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
"gitstore.ru/tolikproh/sirius/internal/model"
)
func NewLogger(cfg *model.Config) *logrus.Logger {
logFile := "sirius_server.log"
log := logrus.New()
logLevel := model.ValidLogLevel(cfg)
log.SetLevel(logLevel)
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: cfg.Srv.LogPath + "/" + logFile,
MaxSize: 1, // megabytes
MaxBackups: 7, // amouts
MaxAge: 1, //days
Level: logLevel,
Formatter: &logrus.JSONFormatter{},
})
if err != nil {
logrus.Fatalf("Failed to initialize file rotate hook: %v", err)
}
//log.SetOutput(colorable.NewColorableStdout())
log.SetOutput(io.Discard)
// log.SetFormatter(&logrus.TextFormatter{
// PadLevelText: true,
// ForceColors: true,
// FullTimestamp: true,
// TimestampFormat: "2006-01-02 15:04:05",
// })
log.AddHook(rotateFileHook)
return log
}