homebox/backend/app/api/logger.go

42 lines
847 B
Go
Raw Permalink Normal View History

2022-09-04 03:27:02 +00:00
package main
import (
"os"
"strings"
"github.com/hay-kot/homebox/backend/internal/config"
2022-09-04 03:27:02 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setupLogger initializes the zerolog config
// for the shared logger.
func (a *app) setupLogger() {
// Logger Init
// zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
2022-09-14 17:35:23 +00:00
if a.conf.Log.Format != config.LogFormatJSON {
2022-09-04 03:27:02 +00:00
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
log.Level(getLevel(a.conf.Log.Level))
}
func getLevel(l string) zerolog.Level {
switch strings.ToLower(l) {
case "debug":
return zerolog.DebugLevel
case "info":
return zerolog.InfoLevel
case "warn":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
case "fatal":
return zerolog.FatalLevel
case "panic":
return zerolog.PanicLevel
default:
return zerolog.InfoLevel
}
}