watermill: added proper logger

This commit is contained in:
Tigor Hutasuhut 2024-04-25 13:22:08 +07:00
parent 7e2cb047c2
commit 847b9c1ab9
2 changed files with 40 additions and 2 deletions

View file

@ -17,7 +17,6 @@ import (
"github.com/tigorlazuardi/redmage/pkg/errs" "github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log" "github.com/tigorlazuardi/redmage/pkg/log"
"github.com/ThreeDotsLabs/watermill"
watermillSql "github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql" watermillSql "github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql"
"github.com/ThreeDotsLabs/watermill/message" "github.com/ThreeDotsLabs/watermill/message"
watermillSqlite "github.com/walterwanderley/watermill-sqlite" watermillSqlite "github.com/walterwanderley/watermill-sqlite"
@ -52,7 +51,7 @@ type Dependencies struct {
const downloadTopic = "subreddit_download" const downloadTopic = "subreddit_download"
var watermillLogger = watermill.NewStdLoggerWithOut(os.Stderr, false, false) var watermillLogger = &log.WatermillLogger{}
func New(deps Dependencies) *API { func New(deps Dependencies) *API {
ackDeadline := deps.Config.Duration("download.pubsub.ack.deadline") ackDeadline := deps.Config.Duration("download.pubsub.ack.deadline")

39
pkg/log/watermill.go Normal file
View file

@ -0,0 +1,39 @@
package log
import (
"context"
"log/slog"
"github.com/ThreeDotsLabs/watermill"
)
type WatermillLogger struct {
with []any
}
func (wa *WatermillLogger) Error(msg string, err error, fields watermill.LogFields) {
New(context.Background()).With(wa.with...).With(intoAttrs(fields)...).Err(err).Error(msg)
}
func (wa *WatermillLogger) Info(msg string, fields watermill.LogFields) {
New(context.Background()).With(wa.with...).With(intoAttrs(fields)...).Info(msg)
}
func (wa *WatermillLogger) Debug(msg string, fields watermill.LogFields) {
New(context.Background()).With(wa.with...).With(intoAttrs(fields)...).Debug(msg)
}
func (wa *WatermillLogger) Trace(msg string, fields watermill.LogFields) {
New(context.Background()).With(wa.with...).With(intoAttrs(fields)...).Debug(msg)
}
func (wa *WatermillLogger) With(fields watermill.LogFields) watermill.LoggerAdapter {
return &WatermillLogger{with: append(wa.with, intoAttrs(fields)...)}
}
func intoAttrs(fields watermill.LogFields) (attrs []any) {
for k, v := range fields {
attrs = append(attrs, slog.Any(k, v))
}
return attrs
}