|
|
@ -23,80 +23,42 @@ func LogFieldsToSlogAttrs(in watermill.LogFields) []slog.Attr {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type WrappedSlogger struct {
|
|
|
|
type WrappedSlogger struct {
|
|
|
|
loggers []*slog.Logger
|
|
|
|
*slog.Logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type LogConfig struct {
|
|
|
|
func NewWrappedSlogger(w io.Writer, verbose int) *WrappedSlogger {
|
|
|
|
Format string
|
|
|
|
|
|
|
|
Writer io.Writer
|
|
|
|
|
|
|
|
Level int
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewWrappedSlogger(configs ...LogConfig) *WrappedSlogger {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loggers := make([]*slog.Logger, 0)
|
|
|
|
|
|
|
|
for _, config := range configs {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts := &slog.HandlerOptions{
|
|
|
|
opts := &slog.HandlerOptions{
|
|
|
|
Level: slog.Level(config.Level),
|
|
|
|
Level: slog.Level(verbose),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := slog.NewTextHandler(w, opts)
|
|
|
|
var handler slog.Handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch config.Format {
|
|
|
|
|
|
|
|
case "json":
|
|
|
|
|
|
|
|
handler = slog.NewJSONHandler(config.Writer, opts)
|
|
|
|
|
|
|
|
case "text":
|
|
|
|
|
|
|
|
handler = slog.NewTextHandler(config.Writer, opts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loggers = append(loggers, slog.New(handler))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &WrappedSlogger{
|
|
|
|
return &WrappedSlogger{
|
|
|
|
loggers: loggers,
|
|
|
|
slog.New(handler),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
|
|
|
|
func (wl *WrappedSlogger) Error(msg string, err error, fields watermill.LogFields) {
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
for _, l := range wl.loggers {
|
|
|
|
wl.LogAttrs(nil, LevelTrace, msg, attrs...)
|
|
|
|
l.LogAttrs(nil, slog.LevelError, msg, attrs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
|
|
|
|
func (wl *WrappedSlogger) Info(msg string, fields watermill.LogFields) {
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
for _, l := range wl.loggers {
|
|
|
|
wl.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
|
|
|
|
l.LogAttrs(nil, slog.LevelInfo, msg, attrs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
|
|
|
|
func (wl *WrappedSlogger) Debug(msg string, fields watermill.LogFields) {
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
for _, l := range wl.loggers {
|
|
|
|
wl.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
|
|
|
|
l.LogAttrs(nil, slog.LevelDebug, msg, attrs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
|
|
|
|
func (wl *WrappedSlogger) Trace(msg string, fields watermill.LogFields) {
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
for _, l := range wl.loggers {
|
|
|
|
wl.LogAttrs(nil, LevelTrace, msg, attrs...)
|
|
|
|
l.LogAttrs(nil, LevelTrace, msg, attrs...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
|
|
|
|
func (wl *WrappedSlogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
attrs := LogFieldsToSlogAttrs(fields)
|
|
|
|
loggerArgs := make([]any, len(attrs))
|
|
|
|
l := slog.Default().With(attrs)
|
|
|
|
for idx, attr := range attrs {
|
|
|
|
return &WrappedSlogger{l}
|
|
|
|
loggerArgs[idx] = attr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
newLoggers := make([]*slog.Logger, 0)
|
|
|
|
|
|
|
|
for _, l := range wl.loggers {
|
|
|
|
|
|
|
|
newLoggers = append(newLoggers, l.With(loggerArgs...))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &WrappedSlogger{loggers: newLoggers}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|