Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

NOISSUE - Fix: messaging handler logger #280

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions pkg/messaging/handler/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,69 @@ type loggingMiddleware struct {

// AuthConnect implements session.Handler.
func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) {
return lm.logAction(ctx, "AuthConnect", nil)
defer lm.logAction(ctx, "AuthConnect", nil, time.Now(), err)
return lm.svc.AuthConnect(ctx)
}

// AuthPublish implements session.Handler.
func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) {
return lm.logAction(ctx, "AuthPublish", &[]string{*topic})
defer lm.logAction(ctx, "AuthPublish", &[]string{*topic}, time.Now(), err)
return lm.svc.AuthPublish(ctx, topic, payload)
}

// AuthSubscribe implements session.Handler.
func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) {
return lm.logAction(ctx, "AuthSubscribe", topics)
defer lm.logAction(ctx, "AuthSubscribe", topics, time.Now(), err)
return lm.svc.AuthSubscribe(ctx, topics)
}

// Connect implements session.Handler.
func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) {
return lm.logAction(ctx, "Connect", nil)
defer lm.logAction(ctx, "Connect", nil, time.Now(), err)
return lm.svc.Connect(ctx)
}

// Disconnect implements session.Handler.
func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) {
return lm.logAction(ctx, "Disconnect", nil)
defer lm.logAction(ctx, "Disconnect", nil, time.Now(), err)
return lm.svc.Disconnect(ctx)
}

// Publish logs the publish request. It logs the time it took to complete the request.
// If the request fails, it logs the error.
func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) {
return lm.logAction(ctx, "Publish", &[]string{*topic})
defer lm.logAction(ctx, "Publish", &[]string{*topic}, time.Now(), err)
return lm.svc.Publish(ctx, topic, payload)
}

// Subscribe implements session.Handler.
func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) {
return lm.logAction(ctx, "Subscribe", topics)
defer lm.logAction(ctx, "Subscribe", topics, time.Now(), err)
return lm.svc.Subscribe(ctx, topics)
}

// Unsubscribe implements session.Handler.
func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) (err error) {
return lm.logAction(ctx, "Unsubscribe", topics)
defer lm.logAction(ctx, "Unsubscribe", topics, time.Now(), err)
return lm.svc.Unsubscribe(ctx, topics)
}

// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware(svc session.Handler, logger *slog.Logger) session.Handler {
return &loggingMiddleware{logger, svc}
}

func (lm *loggingMiddleware) logAction(ctx context.Context, action string, topics *[]string) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
}
if topics != nil {
args = append(args, slog.Any("topics", *topics))
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn(action+"() failed to complete successfully", args...)
return
}
lm.logger.Info(action+"() completed successfully", args...)
}(time.Now())

return nil
func (lm *loggingMiddleware) logAction(ctx context.Context, action string, topics *[]string, t time.Time, err error) {
args := []any{
slog.String("duration", time.Since(t).String()),
}
if topics != nil {
args = append(args, slog.Any("topics", *topics))
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn(action+" failed to complete successfully", args...)
return
}
lm.logger.Info(action+" completed successfully", args...)
}
Loading