diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-10-09 08:58:31 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-10-09 08:58:31 -0600 |
commit | 20be557b7bc3d7ada78411b7b279c9f7580d0bc7 (patch) | |
tree | 2a7f6640b0b389ad79fe18a8906642be493aaf60 /logging | |
parent | cedcf58ea7d729acb6ed1a9ab7aec1ae38aed102 (diff) |
logging.Base()
Allow users to get access to the base logger *before* it is wrapped with the various levels.
This provides a single override point to filter or redirect all logging.
Diffstat (limited to 'logging')
-rw-r--r-- | logging/logger.go | 54 |
1 files changed, 33 insertions, 21 deletions
diff --git a/logging/logger.go b/logging/logger.go index 87b5985..084bea0 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -2,6 +2,7 @@ package logging import ( "os" + "sync" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -13,31 +14,42 @@ type Logger interface { Log(keyvals ...any) error } -// DefaultLoggers produces helpful base loggers for each level. -// -// They write logfmt to standard out, annotated with ANSI colors depending on the level. -func DefaultLoggers() (debug, info, warn, error Logger) { - base := term.NewLogger(os.Stdout, log.NewLogfmtLogger, func(keyvals ...any) term.FgBgColor { - for i := 0; i < len(keyvals)-1; i += 2 { - if keyvals[i] != "level" { - continue - } +var ( + once sync.Once + baselogger Logger +) - switch keyvals[i+1] { - case level.DebugValue(): - return term.FgBgColor{Fg: term.Gray} - case level.InfoValue(): - return term.FgBgColor{Fg: term.Green} - case level.WarnValue(): - return term.FgBgColor{Fg: term.Yellow} - case level.ErrorValue(): - return term.FgBgColor{Fg: term.Red} +func Base() Logger { + once.Do(func() { + base := term.NewLogger(os.Stdout, log.NewLogfmtLogger, func(keyvals ...any) term.FgBgColor { + for i := 0; i < len(keyvals)-1; i += 2 { + if keyvals[i] != "level" { + continue + } + + switch keyvals[i+1] { + case level.DebugValue(): + return term.FgBgColor{Fg: term.Gray} + case level.InfoValue(): + return term.FgBgColor{Fg: term.Green} + case level.WarnValue(): + return term.FgBgColor{Fg: term.Yellow} + case level.ErrorValue(): + return term.FgBgColor{Fg: term.Red} + } } - } - return term.FgBgColor{} + return term.FgBgColor{} + }) + baselogger = log.NewSyncLogger(base) }) - base = log.NewSyncLogger(base) + return baselogger +} +// DefaultLoggers produces helpful base loggers for each level. +// +// They write logfmt to standard out, annotated with ANSI colors depending on the level. +func DefaultLoggers() (debug, info, warn, error Logger) { + base := Base() return level.Debug(base), level.Info(base), level.Warn(base), level.Error(base) } |