diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-23 22:15:16 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-23 22:15:16 -0700 |
commit | 0480e066a3f1ae97dbab8fcb6303589eb0fa724c (patch) | |
tree | 1aa347eb3691a29987475fcf6049343aa227a365 /logging/logger.go | |
parent | df57a12539030297b3254bc81f5696691cbc9c6f (diff) |
logging library up to top level
Diffstat (limited to 'logging/logger.go')
-rw-r--r-- | logging/logger.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/logging/logger.go b/logging/logger.go new file mode 100644 index 0000000..f80a1ae --- /dev/null +++ b/logging/logger.go @@ -0,0 +1,43 @@ +package logging + +import ( + "os" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "github.com/go-kit/log/term" +) + +// Logger records log lines to an output. +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 + } + + switch keyvals[i+1] { + case level.DebugValue(): + return term.FgBgColor{Fg: term.DarkGray} + 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{} + }) + base = log.NewSyncLogger(base) + + return level.Debug(base), level.Info(base), level.Warn(base), level.Error(base) +} |