From a098b880ed63b29c2d196d4f924e86b6655134d7 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 12 Oct 2023 12:01:58 +0200 Subject: [PATCH] Replace more logrus use with slog --- core/hooks/commit_field_changes.go | 12 ++++++------ core/hooks/update_campaign_events.go | 12 ++++++------ core/msgio/android.go | 4 ++-- core/msgio/courier.go | 4 ++-- core/msgio/send.go | 12 ++++++------ core/runner/runner.go | 3 ++- core/search/groups.go | 4 ++-- core/search/search.go | 22 ++++++++++++++-------- 8 files changed, 40 insertions(+), 33 deletions(-) diff --git a/core/hooks/commit_field_changes.go b/core/hooks/commit_field_changes.go index 8ab64b806..28df1dfba 100644 --- a/core/hooks/commit_field_changes.go +++ b/core/hooks/commit_field_changes.go @@ -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 @@ -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 } diff --git a/core/hooks/update_campaign_events.go b/core/hooks/update_campaign_events.go index a991316db..e8d369f95 100644 --- a/core/hooks/update_campaign_events.go +++ b/core/hooks/update_campaign_events.go @@ -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 @@ -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 diff --git a/core/msgio/android.go b/core/msgio/android.go index 75cdde97a..8f4727d6c 100644 --- a/core/msgio/android.go +++ b/core/msgio/android.go @@ -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 @@ -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 } diff --git a/core/msgio/courier.go b/core/msgio/courier.go index 11e333868..947c3bd7b 100644 --- a/core/msgio/courier.go +++ b/core/msgio/courier.go @@ -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{ @@ -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 } diff --git a/core/msgio/send.go b/core/msgio/send.go index f1290eb76..efe058f70 100644 --- a/core/msgio/send.go +++ b/core/msgio/send.go @@ -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 { @@ -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) } } } @@ -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 { @@ -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)...) } @@ -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) @@ -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 diff --git a/core/runner/runner.go b/core/runner/runner.go index c20859257..ebfb47a25 100644 --- a/core/runner/runner.go +++ b/core/runner/runner.go @@ -17,6 +17,7 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/exp/maps" + "golang.org/x/exp/slog" ) const ( @@ -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() diff --git a/core/search/groups.go b/core/search/groups.go index 72e82c312..0900fbdee 100644 --- a/core/search/groups.go +++ b/core/search/groups.go @@ -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 @@ -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) } } diff --git a/core/search/search.go b/core/search/search.go index e80328de0..274c3711f 100644 --- a/core/search/search.go +++ b/core/search/search.go @@ -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 @@ -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 } @@ -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 }