Improve logging

This commit is contained in:
Torjus Håkestad 2021-09-02 03:05:04 +02:00
parent 64feefa7a5
commit 6e15d9835c

View File

@ -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()