From 6e15d9835c39a099cef1ab3768ef05211062261f Mon Sep 17 00:00:00 2001 From: = Date: Thu, 2 Sep 2021 03:05:04 +0200 Subject: [PATCH] Improve logging --- cmd/dogtamer.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cmd/dogtamer.go b/cmd/dogtamer.go index c9fe473..e9bd8de 100644 --- a/cmd/dogtamer.go +++ b/cmd/dogtamer.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "os/signal" + "strings" "github.com/urfave/cli/v2" "github.uio.no/torjus/dogtamer" @@ -57,11 +58,14 @@ func main() { } func ActionServe(c *cli.Context) error { - logger := setupServerLogger() + // Setup temporary logger, incase config could not be read + logger := setupServerLogger("INFO") cfg, err := config.FromDefaultLocations() if err != nil { logger.Warn("No config file found.") } + // Setup proper logger + logger = setupServerLogger(cfg.LogLevel) ctx, cancel := context.WithCancel(context.Background()) // Setup RTMP-server @@ -106,7 +110,7 @@ func ActionServe(c *cli.Context) error { return nil } -func setupServerLogger() *zap.SugaredLogger { +func setupServerLogger(loglevel string) *zap.SugaredLogger { logEncoderConfig := zap.NewProductionEncoderConfig() logEncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder logEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder @@ -119,6 +123,19 @@ func setupServerLogger() *zap.SugaredLogger { ErrorOutputPaths: []string{"stdout"}, Encoding: "console", EncoderConfig: logEncoderConfig, + DisableCaller: true, + } + + switch strings.ToUpper(loglevel) { + case "DEBUG": + rootLoggerConfig.DisableCaller = false + rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + case "INFO": + rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel) + case "WARN", "WARNING": + rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.WarnLevel) + case "ERR", "ERROR": + rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel) } rootLogger, err := rootLoggerConfig.Build()