From 921c058077b50e68b050b13e61d7d6dee66aa4dd Mon Sep 17 00:00:00 2001 From: ucpr Date: Fri, 29 Dec 2023 18:19:33 +0900 Subject: [PATCH] chore: use log attr aliases --- cmd/main.go | 7 +++---- internal/app/streamer.go | 3 +-- internal/mongo/change_stream.go | 11 +++++------ internal/persistent/log.go | 3 +-- pkg/log/attr.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 pkg/log/attr.go diff --git a/cmd/main.go b/cmd/main.go index 24acde4..a3fa64a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,7 +3,6 @@ package main import ( "context" "errors" - "log/slog" "net/http" "os/signal" "syscall" @@ -19,9 +18,9 @@ const ( func main() { log.Info("initializing mongo-streamer", - slog.String("version", stamp.BuildVersion), - slog.String("revision", stamp.BuildRevision), - slog.String("timestamp", stamp.BuildTimestamp), + log.Fstring("version", stamp.BuildVersion), + log.Fstring("revision", stamp.BuildRevision), + log.Fstring("timestamp", stamp.BuildTimestamp), ) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) defer stop() diff --git a/internal/app/streamer.go b/internal/app/streamer.go index d704f52..dff2dc0 100644 --- a/internal/app/streamer.go +++ b/internal/app/streamer.go @@ -2,7 +2,6 @@ package app import ( "context" - "log/slog" "github.com/ucpr/mongo-streamer/internal/pubsub" "github.com/ucpr/mongo-streamer/pkg/log" @@ -26,6 +25,6 @@ func (e *Handler) EventHandler(ctx context.Context, event []byte) error { if err != nil { return err } - log.Info("successful publish event", slog.String("id", id)) + log.Info("successful publish event", log.Fstring("id", id)) return nil } diff --git a/internal/mongo/change_stream.go b/internal/mongo/change_stream.go index ed8eb5b..93c1e70 100644 --- a/internal/mongo/change_stream.go +++ b/internal/mongo/change_stream.go @@ -3,7 +3,6 @@ package mongo import ( "context" "errors" - "log/slog" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -75,7 +74,7 @@ func NewChangeStream(ctx context.Context, params ChangeStreamParams, opts ...Cha if err != nil { // if resume token is not found, reset resume token and retry if errors.Is(err, mongo.ErrMissingResumeToken) { - log.Warn("resume token is not found, reset resume token and retry", slog.String("db", db), slog.String("col", col)) + log.Warn("resume token is not found, reset resume token and retry", log.Fstring("db", db), log.Fstring("col", col)) chopts.SetResumeAfter(nil) if err := params.Storage.Clear(); err != nil { return nil, err @@ -108,7 +107,7 @@ func (c *ChangeStream) Run(ctx context.Context) { var streamObject bson.M if err := c.cs.Decode(&streamObject); err != nil { mmetric.HandleChangeEventFailed(c.db, c.col) - log.Error("failed to decode steream object", slog.String("err", err.Error())) + log.Error("failed to decode steream object", log.Ferror(err)) continue } @@ -116,14 +115,14 @@ func (c *ChangeStream) Run(ctx context.Context) { jb, err := bson.MarshalExtJSON(streamObject, false, false) if err != nil { mmetric.HandleChangeEventFailed(c.db, c.col) - log.Error("failed to marshal stream object", slog.String("err", err.Error())) + log.Error("failed to marshal stream object", log.Ferror(err)) continue } mmetric.ReceiveBytes(c.db, c.col, len(jb)) if err := c.handler(context.Background(), jb); err != nil { mmetric.HandleChangeEventFailed(c.db, c.col) - log.Error("failed to handle change stream", slog.String("err", err.Error())) + log.Error("failed to handle change stream", log.Ferror(err)) // TODO: If handle fails, the process is repeated again continue } @@ -131,7 +130,7 @@ func (c *ChangeStream) Run(ctx context.Context) { // save resume token if err := c.tokenManager.Set(c.resumeToken()); err != nil { - log.Error("failed to save resume token", slog.String("err", err.Error())) + log.Error("failed to save resume token", log.Ferror(err)) continue } } diff --git a/internal/persistent/log.go b/internal/persistent/log.go index 4761f82..e0ef920 100644 --- a/internal/persistent/log.go +++ b/internal/persistent/log.go @@ -2,7 +2,6 @@ package persistent import ( "context" - "log/slog" "github.com/ucpr/mongo-streamer/pkg/log" ) @@ -20,7 +19,7 @@ func NewLogWriter() *Log { } func (l *Log) Write(s string) error { - log.Info("persistent: write data", slog.String("data", s)) + log.Info("persistent: write data", log.Fstring("data", s)) return nil } diff --git a/pkg/log/attr.go b/pkg/log/attr.go new file mode 100644 index 0000000..9764d23 --- /dev/null +++ b/pkg/log/attr.go @@ -0,0 +1,31 @@ +package log + +import ( + "log/slog" +) + +var ( + // Fstring is alias for slog.String + Fstring = slog.String + // Fint is alias for slog.Int + Fint = slog.Int + // Fuint is alias for slog.Uint + Fint64 = slog.Int64 + // Fuint64 is alias for slog.Uint64 + Fuint64 = slog.Uint64 + // Ffloat32 is alias for slog.Float32 + Ffloat64 = slog.Float64 + // Ffloat64 is alias for slog.Float64 + Fbool = slog.Bool + // Ftime is alias for slog.Time + Ftime = slog.Time + // Fduration is alias for slog.Duration + Fduration = slog.Duration + // Fany is alias for slog.Any + Fany = slog.Any +) + +// Ferror is return error attribute +func Ferror(err error) slog.Attr { + return slog.String("error", err.Error()) +}