From a50f5b648daad02c6ac900b133dbe17bc864b288 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Wed, 3 Jan 2024 08:31:25 -0700 Subject: [PATCH] portal: add mxid to logger Signed-off-by: Sumner Evans --- portal.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/portal.go b/portal.go index 301e77db..4e21f43b 100644 --- a/portal.go +++ b/portal.go @@ -253,10 +253,15 @@ var signalFormatParams *signalfmt.FormatParams var matrixFormatParams *matrixfmt.HTMLParser func (br *SignalBridge) NewPortal(dbPortal *database.Portal) *Portal { + logCtx := br.ZLog.With().Str("chat_id", dbPortal.ChatID) + if dbPortal.MXID != "" { + logCtx = logCtx.Stringer("portal_mxid", dbPortal.MXID) + } + portal := &Portal{ Portal: dbPortal, bridge: br, - log: br.ZLog.With().Str("chat_id", dbPortal.ChatID).Logger(), + log: logCtx.Logger(), signalMessages: make(chan portalSignalMessage, br.Config.Bridge.PortalMessageBuffer), matrixMessages: make(chan portalMatrixMessage, br.Config.Bridge.PortalMessageBuffer), @@ -561,7 +566,7 @@ func (portal *Portal) handleMatrixReaction(ctx context.Context, sender *User, ev err = portal.sendSignalMessage(ctx, msg, sender, evt.ID) if err != nil { portal.sendMessageStatusCheckpointFailed(evt, err) - portal.log.Error().Msgf("Failed to send reaction %s", evt.ID) + portal.log.Error().Stringer("event_id", evt.ID).Msg("Failed to send reaction") return } @@ -620,13 +625,12 @@ func (portal *Portal) sendSignalMessage(ctx context.Context, msg *signalpb.Conte log.Debug().Msg("Sending event to Signal") // Check to see if portal.ChatID is a standard UUID (with dashes) - var err error if _, uuidErr := uuid.Parse(portal.ChatID); uuidErr == nil { // this is a 1:1 chat result := signalmeow.SendMessage(ctx, sender.SignalDevice, portal.ChatID, msg) if !result.WasSuccessful { - err = result.FailedSendResult.Error - log.Err(err).Msg("Error sending event to Signal") + log.Err(result.Error).Msg("Error sending event to Signal") + return result.Error } } else { // this is a group chat @@ -653,14 +657,14 @@ func (portal *Portal) sendSignalMessage(ctx context.Context, msg *signalpb.Conte log.Debug().Msg("No successes or failures - Probably sent to myself") } else if len(result.SuccessfullySentTo) == 0 { log.Error().Msg("Failed to send event to all members of Signal group") - err = errors.New("failed to send to any members of Signal group") + return errors.New("failed to send to any members of Signal group") } else if len(result.SuccessfullySentTo) < totalRecipients { log.Warn().Msg("Only sent event to some members of Signal group") } else { - log.Debug().Msgf("Sent event to all members of Signal group") + log.Debug().Msg("Sent event to all members of Signal group") } } - return err + return nil } func (portal *Portal) sendMessageStatusCheckpointSuccess(evt *event.Event) { @@ -1164,7 +1168,7 @@ func (portal *Portal) setTyping(userIDs []id.UserID, isTyping bool) { var err error if _, uuidErr := uuid.Parse(portal.ChatID); uuidErr == nil { // this is a 1:1 chat - portal.log.Debug().Msgf("Sending Typing event to Signal %s", portal.ChatID) + portal.log.Debug().Msg("Sending Typing event to Signal") ctx := context.Background() typingMessage := signalmeow.TypingMessage(isTyping) result := signalmeow.SendMessage(ctx, user.SignalDevice, portal.ChatID, typingMessage) @@ -1374,7 +1378,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, meta *any) error { }) } - creationContent := make(map[string]interface{}) + creationContent := make(map[string]any) if !portal.bridge.Config.Bridge.FederateRooms { creationContent["m.federate"] = false } @@ -1424,12 +1428,13 @@ func (portal *Portal) CreateMatrixRoom(user *User, meta *any) error { if err != nil { portal.log.Err(err).Msg("Failed to save created portal mxid") } - portal.log.Info().Msgf("Created matrix room %s", portal.MXID) + portal.log = portal.log.With().Stringer("portal_mxid", portal.MXID).Logger() + portal.log.Info().Msg("Created matrix room") if portal.Encrypted && portal.IsPrivateChat() { err = portal.bridge.Bot.EnsureJoined(portal.MXID, appservice.EnsureJoinedParams{BotOverride: portal.MainIntent().Client}) if err != nil { - portal.log.Error().Err(err).Msg("Failed to ensure bridge bot is joined to private chat portal") + portal.log.Err(err).Msg("Failed to ensure bridge bot is joined to private chat portal") } } @@ -1440,10 +1445,10 @@ func (portal *Portal) CreateMatrixRoom(user *User, meta *any) error { //portal.syncParticipants(user, channel.Recipients) if portal.IsPrivateChat() { - portal.log.Debug().Msgf("Portal is private chat, updating direct chats: %s", portal.MXID) + portal.log.Debug().Msg("Portal is private chat, updating direct chats") puppet := user.bridge.GetPuppetBySignalID(portal.Receiver) if puppet == nil { - portal.log.Error().Msgf("Failed to find puppet for portal receiver %s", portal.Receiver) + portal.log.Error().Stringer("portal_receiver", portal.Receiver).Msg("Failed to find puppet for portal receiver") return nil }