Skip to content

Commit

Permalink
chore: use log attr aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ucpr committed Dec 29, 2023
1 parent 36e5368 commit 921c058
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
7 changes: 3 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os/signal"
"syscall"
Expand All @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions internal/app/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"context"
"log/slog"

"github.com/ucpr/mongo-streamer/internal/pubsub"
"github.com/ucpr/mongo-streamer/pkg/log"
Expand All @@ -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
}
11 changes: 5 additions & 6 deletions internal/mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mongo
import (
"context"
"errors"
"log/slog"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -108,30 +107,30 @@ 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
}

// marshal stream object to json
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
}
mmetric.HandleChangeEventSuccess(c.db, c.col)

// 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
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/persistent/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package persistent

import (
"context"
"log/slog"

"github.com/ucpr/mongo-streamer/pkg/log"
)
Expand All @@ -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
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/log/attr.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 921c058

Please sign in to comment.