Skip to content

Commit

Permalink
zerolog: use Stringer() where appropriate
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Jan 3, 2024
1 parent eebcbdc commit ffb036a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions chatwoot-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func SendMessage(ctx context.Context, roomID id.RoomID, content *event.MessageEventContent, extraContent ...map[string]any) (resp *mautrix.RespSendEvent, err error) {
log := zerolog.Ctx(ctx).With().Str("room_id", roomID.String()).Logger()
log := zerolog.Ctx(ctx).With().Stringer("room_id", roomID).Logger()
ctx = log.WithContext(ctx)

wrappedContent := event.Content{Parsed: content}
Expand All @@ -48,7 +48,7 @@ func SendMessage(ctx context.Context, roomID id.RoomID, content *event.MessageEv
return r, err
}

func HandleWebhook(w http.ResponseWriter, r *http.Request) {
func HandleWebhook(_ http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
ctx := log.WithContext(context.Background())

Expand Down Expand Up @@ -297,7 +297,7 @@ func HandleMessageCreated(ctx context.Context, mc chatwootapi.MessageCreated) er
return fmt.Errorf("invalid start new chat response: %s", sncResp.Error)
}

log = log.With().Str("room_id", sncResp.RoomID.String()).Logger()
log = log.With().Stringer("room_id", sncResp.RoomID).Logger()
log.Info().Msg("created new chat for conversation")
roomID = sncResp.RoomID

Expand All @@ -313,7 +313,7 @@ func HandleMessageCreated(ctx context.Context, mc chatwootapi.MessageCreated) er
return err
}
}
log = log.With().Str("room_id", roomID.String()).Logger()
log = log.With().Stringer("room_id", roomID).Logger()
ctx = log.WithContext(ctx)

// Acquire the lock, so that we don't have race conditions with the matrix
Expand Down
6 changes: 3 additions & 3 deletions chatwoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func main() {

getLogger := func(evt *event.Event) zerolog.Logger {
return log.With().
Str("event_type", evt.Type.String()).
Str("sender", evt.Sender.String()).
Stringer("event_type", &evt.Type).
Stringer("sender", evt.Sender).
Str("room_id", string(evt.RoomID)).
Str("event_id", string(evt.ID)).
Logger()
Expand Down Expand Up @@ -304,7 +304,7 @@ func main() {
}

func backfillConversationForRoom(ctx context.Context, roomID id.RoomID) error {
log := zerolog.Ctx(ctx).With().Str("room_id", roomID.String()).Logger()
log := zerolog.Ctx(ctx).With().Stringer("room_id", roomID).Logger()

log.Info().Msg("Creating conversation for room")

Expand Down
2 changes: 1 addition & 1 deletion chatwootapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (api *ChatwootAPI) MakeUri(endpoint string) string {
func (api *ChatwootAPI) CreateContact(ctx context.Context, userID id.UserID, name string) (int, error) {
log := zerolog.Ctx(ctx).With().
Str("component", "create_contact").
Str("user_id", userID.String()).
Stringer("user_id", userID).
Logger()

if name == "" {
Expand Down
2 changes: 1 addition & 1 deletion database/chatwoot-conversation-to-matrix-room.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (store *Database) GetMatrixRoomFromChatwootConversation(ctx context.Context
func (store *Database) UpdateMostRecentEventIDForRoom(ctx context.Context, roomID id.RoomID, mostRecentEventID id.EventID) error {
log := zerolog.Ctx(ctx).With().
Str("component", "update_most_recent_event_id_for_room").
Str("most_recent_event_id", mostRecentEventID.String()).
Stringer("most_recent_event_id", mostRecentEventID).
Logger()
ctx = log.WithContext(ctx)

Expand Down
2 changes: 1 addition & 1 deletion database/chatwoot-message-to-matrix-event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (store *Database) SetChatwootMessageIDForMatrixEvent(ctx context.Context, eventID id.EventID, chatwootMessageID int) error {
log := zerolog.Ctx(ctx).With().
Str("event_id", eventID.String()).
Stringer("event_id", eventID).
Int("chatwoot_message_id", chatwootMessageID).
Logger()
ctx = log.WithContext(ctx)
Expand Down
26 changes: 13 additions & 13 deletions matrix-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var createRoomLock sync.Mutex = sync.Mutex{}
func createChatwootConversation(ctx context.Context, roomID id.RoomID, contactMXID id.UserID, customAttrs map[string]string) (int, error) {
log := zerolog.Ctx(ctx).With().
Str("component", "create_chatwoot_conversation").
Str("room_id", roomID.String()).
Str("contact_mxid", contactMXID.String()).
Stringer("room_id", roomID).
Stringer("contact_mxid", contactMXID).
Interface("custom_attrs", customAttrs).
Logger()
ctx = log.WithContext(ctx)
Expand Down Expand Up @@ -165,8 +165,8 @@ var deviceVersionRegex = regexp.MustCompile(`(\S+)( \(last updated at .*\))?`)
func HandleBeeperClientInfo(ctx context.Context, evt *event.Event) error {
log := zerolog.Ctx(ctx).With().
Str("component", "handle_beeper_client_info").
Str("room_id", evt.RoomID.String()).
Str("event_id", evt.ID.String()).
Stringer("room_id", evt.RoomID).
Stringer("event_id", evt.ID).
Logger()
ctx = log.WithContext(ctx)

Expand Down Expand Up @@ -220,8 +220,8 @@ var rageshakeIssueRegex = regexp.MustCompile(`[A-Z]{1,5}-\d+`)
func HandleMessage(ctx context.Context, _ mautrix.EventSource, evt *event.Event) {
log := zerolog.Ctx(ctx).With().
Str("component", "handle_message").
Str("room_id", evt.RoomID.String()).
Str("event_id", evt.ID.String()).
Stringer("room_id", evt.RoomID).
Stringer("event_id", evt.ID).
Logger()
ctx = log.WithContext(ctx)

Expand Down Expand Up @@ -344,8 +344,8 @@ func GetOrCreateChatwootConversation(ctx context.Context, roomID id.RoomID, evt
func HandleReaction(ctx context.Context, _ mautrix.EventSource, evt *event.Event) {
log := zerolog.Ctx(ctx).With().
Str("component", "handle_reaction").
Str("room_id", evt.RoomID.String()).
Str("event_id", evt.ID.String()).
Stringer("room_id", evt.RoomID).
Stringer("event_id", evt.ID).
Logger()
ctx = log.WithContext(ctx)

Expand Down Expand Up @@ -448,8 +448,8 @@ func HandleMatrixMessageContent(ctx context.Context, evt *event.Event, conversat
log := zerolog.Ctx(ctx).With().
Str("component", "handle_matrix_message_content").
Int("conversation_id", conversationID).
Str("room_id", evt.RoomID.String()).
Str("event_id", evt.ID.String()).
Stringer("room_id", evt.RoomID).
Stringer("event_id", evt.ID).
Logger()
ctx = log.WithContext(ctx)

Expand Down Expand Up @@ -553,8 +553,8 @@ func HandleMatrixMessageContent(ctx context.Context, evt *event.Event, conversat

func HandleRedaction(ctx context.Context, _ mautrix.EventSource, evt *event.Event) {
log := zerolog.Ctx(ctx).With().
Str("room_id", evt.RoomID.String()).
Str("event_id", evt.ID.String()).
Stringer("room_id", evt.RoomID).
Stringer("event_id", evt.ID).
Logger()
ctx = log.WithContext(ctx)

Expand All @@ -571,7 +571,7 @@ func HandleRedaction(ctx context.Context, _ mautrix.EventSource, evt *event.Even

messageIDs, err := stateStore.GetChatwootMessageIDsForMatrixEventID(ctx, evt.Redacts)
if err != nil || len(messageIDs) == 0 {
log.Err(err).Str("redacts", evt.Redacts.String()).Msg("no Chatwoot message for redacted event")
log.Err(err).Stringer("redacts", evt.Redacts).Msg("no Chatwoot message for redacted event")
return
}

Expand Down

0 comments on commit ffb036a

Please sign in to comment.