Add common logger helper

This commit is contained in:
2023-07-01 19:57:36 -07:00
parent cf61f70d95
commit cc6a6c0320
5 changed files with 140 additions and 0 deletions

37
logger/stdlog.go Normal file
View File

@@ -0,0 +1,37 @@
package logger
import (
"fmt"
"golang.org/x/exp/slog"
)
type stdLoggerish struct {
key string
log *slog.Logger
f func(string, ...any)
}
func NewStdLog(key string, debug bool, log *slog.Logger) *stdLoggerish {
if log == nil {
log = Setup()
}
sl := &stdLoggerish{
key: key,
log: log,
}
sl.f = log.Info
if debug {
sl.f = log.Debug
}
return sl
}
func (l stdLoggerish) Println(msg ...interface{}) {
l.f(l.key, "msg", msg)
}
func (l stdLoggerish) Printf(msg string, args ...interface{}) {
l.f(l.key, "msg", fmt.Sprintf(msg, args...))
}