geck-go/logging/init.go

42 lines
975 B
Go
Raw Normal View History

2021-12-01 19:22:07 +00:00
package logging
import (
"io"
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var levelsMapping = map[zerolog.Level]sentry.Level{
zerolog.DebugLevel: sentry.LevelDebug,
zerolog.InfoLevel: sentry.LevelInfo,
zerolog.WarnLevel: sentry.LevelWarning,
zerolog.ErrorLevel: sentry.LevelError,
zerolog.FatalLevel: sentry.LevelFatal,
zerolog.PanicLevel: sentry.LevelFatal,
}
// Initialize configures ZeroLog with defaults.
func Initialize() {
cw := zerolog.NewConsoleWriter()
cw.Out = os.Stderr
cw.TimeFormat = "15:04:05"
sw := NewSentryWriter()
w := io.MultiWriter(cw, sw)
log.Logger = zerolog.New(w).With().Timestamp().Logger()
zerolog.DurationFieldUnit = time.Millisecond
}
// SetGlobalLevel sets the global log level according to the passed debug flag.
func SetGlobalLevel(debug bool) {
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
}