Skip to content

Commit

Permalink
NOISSUE - Fix messaging handler logger (#280)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Jan 26, 2024
1 parent 15314a5 commit e2616ec
Showing 1 changed file with 29 additions and 25 deletions.
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...)
}

0 comments on commit e2616ec

Please sign in to comment.