From 840008df2d11bc76e2e5e156dcb13bd16b25e3d9 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Mon, 19 Aug 2024 14:12:00 +0200 Subject: [PATCH] feat(internal/logger): add logger package with different style presets --- internal/logger/logger.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 internal/logger/logger.go diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..fbcc944 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,28 @@ +package logger + +import ( + "fmt" + "io" + "path/filepath" + "time" + + "github.com/rs/zerolog" +) + +func CliLoggerLayout(out io.Writer) zerolog.Logger { + zerolog.CallerMarshalFunc = func(_ uintptr, file string, line int) string { + return fmt.Sprintf("%s:%d", filepath.Base(file), line) + } + + if zerolog.GlobalLevel() == zerolog.DebugLevel { + debugOutput := zerolog.ConsoleWriter{Out: out, TimeFormat: time.RFC3339} + return zerolog.New(debugOutput).With().Timestamp().Caller().Logger() + } + + output := zerolog.ConsoleWriter{Out: out, PartsExclude: []string{"time", "level"}} + return zerolog.New(output).With().Logger() +} + +func JSONLoggerLayout(out io.Writer) zerolog.Logger { + return zerolog.New(out).With().Timestamp().Caller().Logger() +}