logging
parent
53739fbc1d
commit
13fdf6160c
@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
)
|
||||
|
||||
func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
|
||||
attrs := make([]slog.Attr, 0)
|
||||
for k, v := range in {
|
||||
attrs = append(attrs, slog.Attr{
|
||||
Key: k,
|
||||
Value: slog.AnyValue(v),
|
||||
})
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
type WrappedSlogger struct {
|
||||
*slog.Logger
|
||||
}
|
||||
|
||||
func NewWrappedSlogger() *WrappedSlogger {
|
||||
return &WrappedSlogger{
|
||||
slog.Default(),
|
||||
}
|
||||
}
|
||||
|
||||
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
|
||||
attrs := LogFieldsToSlogAttrs(fields)
|
||||
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
|
||||
}
|
||||
|
||||
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
|
||||
attrs := LogFieldsToSlogAttrs(fields)
|
||||
wl.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
|
||||
}
|
||||
|
||||
func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
|
||||
attrs := LogFieldsToSlogAttrs(fields)
|
||||
wl.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
|
||||
}
|
||||
|
||||
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
|
||||
attrs := LogFieldsToSlogAttrs(fields)
|
||||
wl.LogAttrs(nil, slog.Level(-5), msg, attrs...)
|
||||
}
|
||||
|
||||
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
|
||||
attrs := LogFieldsToSlogAttrs(fields)
|
||||
l := slog.Default().With(attrs)
|
||||
return &WrappedSlogger{l}
|
||||
|
||||
}
|
Loading…
Reference in New Issue