Skip to content

Commit

Permalink
Replace more logrus use with slog
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Oct 12, 2023
1 parent c3fafb7 commit a098b88
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
12 changes: 6 additions & 6 deletions core/hooks/commit_field_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

// CommitFieldChangesHook is our hook for contact field changes
Expand All @@ -30,11 +30,11 @@ func (h *commitFieldChangesHook) Apply(ctx context.Context, rt *runtime.Runtime,
event := e.(*events.ContactFieldChangedEvent)
field := oa.FieldByKey(event.Field.Key)
if field == nil {
logrus.WithFields(logrus.Fields{
"field_key": event.Field.Key,
"field_name": event.Field.Name,
"session_id": scene.SessionID(),
}).Debug("unable to find field with key, ignoring")
slog.Debug("unable to find field with key, ignoring",
"field_key", event.Field.Key,
"field_name", event.Field.Name,
"session_id", scene.SessionID(),
)
continue
}

Expand Down
12 changes: 6 additions & 6 deletions core/hooks/update_campaign_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

// UpdateCampaignEventsHook is our hook to update any campaign events
Expand Down Expand Up @@ -44,11 +44,11 @@ func (h *updateCampaignEventsHook) Apply(ctx context.Context, rt *runtime.Runtim
case *events.ContactFieldChangedEvent:
field := oa.FieldByKey(event.Field.Key)
if field == nil {
logrus.WithFields(logrus.Fields{
"field_key": event.Field.Key,
"field_name": event.Field.Name,
"session_id": s.SessionID(),
}).Debug("unable to find field with key, ignoring for campaign updates")
slog.Debug("unable to find field with key, ignoring for campaign updates",
"field_key", event.Field.Key,
"field_name", event.Field.Name,
"session_id", s.SessionID(),
)
continue
}
fieldChanges[field.ID()] = true
Expand Down
4 changes: 2 additions & 2 deletions core/msgio/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

// SyncAndroidChannel tries to trigger sync of the given Android channel via FCM
Expand Down Expand Up @@ -37,7 +37,7 @@ func SyncAndroidChannel(fc *fcm.Client, channel *models.Channel) error {
return errors.Wrap(err, "error syncing channel")
}

logrus.WithField("elapsed", time.Since(start)).WithField("channel_uuid", channel.UUID()).Debug("android sync complete")
slog.Debug("android sync complete", "elapsed", time.Since(start), "channel_uuid", channel.UUID())
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions core/msgio/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

var courierHttpClient = &http.Client{
Expand Down Expand Up @@ -221,7 +221,7 @@ func QueueCourierMessages(rc redis.Conn, oa *models.OrgAssets, contactID models.
if err != nil {
return err
}
logrus.WithFields(logrus.Fields{"msgs": len(batch), "contact_id": contactID, "channel_uuid": channel.UUID(), "elapsed": time.Since(start)}).Debug("msgs queued to courier")
slog.Debug("msgs queued to courier", "msgs", len(batch), "contact_id", contactID, "channel_uuid", channel.UUID(), "elapsed", time.Since(start))
}
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions core/msgio/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/edganiukov/fcm"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/runtime"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"golang.org/x/exp/slog"
)

type Send struct {
Expand Down Expand Up @@ -37,7 +37,7 @@ func QueueMessages(ctx context.Context, rt *runtime.Runtime, db models.DBorTx, f
// save an update in the common case)
err := models.MarkMessagesForRequeuing(ctx, db, retry)
if err != nil {
logrus.WithError(err).Error("error marking messages as initializing")
slog.Error("error marking messages as initializing", "error", err)
}
}
}
Expand All @@ -52,7 +52,7 @@ func tryToQueue(ctx context.Context, rt *runtime.Runtime, db models.DBorTx, fc *
for _, batch := range models.ChunkSlice(urnIDs, 1000) {
urns, err := models.LoadContactURNs(ctx, db, batch)
if err != nil {
logrus.WithError(err).Error("error getting contact URNs")
slog.Error("error getting contact URNs", "error", err)
return nil
}
for _, u := range urns {
Expand All @@ -74,7 +74,7 @@ func tryToQueue(ctx context.Context, rt *runtime.Runtime, db models.DBorTx, fc *
for orgID, orgSends := range sendsByOrg {
oa, err := models.GetOrgAssets(ctx, rt, orgID)
if err != nil {
logrus.WithError(err).Error("error getting org assets")
slog.Error("error getting org assets", "error", err)
} else {
queued = append(queued, tryToQueueForOrg(ctx, rt, db, fc, oa, orgSends)...)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func tryToQueueForOrg(ctx context.Context, rt *runtime.Runtime, db models.DBorTx

// just log the error and continue to try - messages that weren't queued will be retried later
if err != nil {
logrus.WithField("channel_uuid", cc.channel.UUID()).WithField("contact_id", cc.contactID).WithError(err).Error("error queuing messages")
slog.Error("error queuing messages", "error", err, "channel_uuid", cc.channel.UUID(), "contact_id", cc.contactID)
} else {
for _, s := range contactSends {
queued = append(queued, s.Msg)
Expand All @@ -140,7 +140,7 @@ func tryToQueueForOrg(ctx context.Context, rt *runtime.Runtime, db models.DBorTx
for channel, msgs := range androidMsgs {
err := SyncAndroidChannel(fc, channel)
if err != nil {
logrus.WithField("channel_uuid", channel.UUID()).WithError(err).Error("error syncing messages")
slog.Error("error syncing messages", "error", err, "channel_uuid", channel.UUID())
}

// even if syncing fails, we consider these messages queued because the device will try to sync by itself
Expand Down
3 changes: 2 additions & 1 deletion core/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"golang.org/x/exp/slog"
)

const (
Expand Down Expand Up @@ -377,7 +378,7 @@ func StartFlowForContacts(

// retry committing our sessions one at a time
if err != nil {
logrus.WithError(err).Debug("failed committing bulk transaction, retrying one at a time")
slog.Debug("failed committing bulk transaction, retrying one at a time", "error", err)

tx.Rollback()

Expand Down
4 changes: 2 additions & 2 deletions core/search/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/olivere/elastic/v7"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

// PopulateSmartGroup calculates which members should be part of a group and populates the contacts
Expand All @@ -34,7 +34,7 @@ func PopulateSmartGroup(ctx context.Context, rt *runtime.Runtime, es *elastic.Cl
// if it was more recent than 10 seconds ago, sleep until it has been 10 seconds
if n.Add(time.Second * 10).After(start) {
sleep := n.Add(time.Second * 10).Sub(start)
logrus.WithField("sleep", sleep).Info("sleeping before evaluating dynamic group")
slog.Info("sleeping before evaluating dynamic group", "sleep", sleep)
time.Sleep(sleep)
}
}
Expand Down
22 changes: 14 additions & 8 deletions core/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/olivere/elastic/v7"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slog"
)

// AssetMapper maps resolved assets in queries to how we identify them in ES which in the case
Expand Down Expand Up @@ -146,7 +146,13 @@ func GetContactIDsForQueryPage(ctx context.Context, rt *runtime.Runtime, oa *mod
return nil, nil, 0, err
}

logrus.WithFields(logrus.Fields{"org_id": oa.OrgID(), "query": query, "elapsed": time.Since(start), "page_count": len(ids), "total_count": results.Hits.TotalHits.Value}).Debug("paged contact query complete")
slog.Debug("paged contact query complete",
"org_id", oa.OrgID(),
"query", query,
"elapsed", time.Since(start),
"page_count", len(ids),
"total_count", results.Hits.TotalHits.Value,
)

return parsed, ids, results.Hits.TotalHits.Value, nil
}
Expand Down Expand Up @@ -186,12 +192,12 @@ func GetContactIDsForQuery(ctx context.Context, rt *runtime.Runtime, oa *models.
for {
results, err := scroll.Do(ctx)
if err == io.EOF {
logrus.WithFields(logrus.Fields{
"org_id": oa.OrgID(),
"query": query,
"elapsed": time.Since(start),
"match_count": len(ids),
}).Debug("contact query complete")
slog.Debug("contact query complete",
"org_id", oa.OrgID(),
"query", query,
"elapsed", time.Since(start),
"match_count", len(ids),
)

return ids, nil
}
Expand Down

0 comments on commit a098b88

Please sign in to comment.