From a3bf0e648e75dc68200861c63780bfd8109e5bf7 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 15 Jan 2024 09:12:45 +0300 Subject: [PATCH 01/23] update middleware Signed-off-by: Musilah --- bootstrap/api/logging.go | 220 +++++++++++++++++---- certs/api/logging.go | 70 +++++-- coap/api/logging.go | 52 ++++- consumers/notifiers/api/logging.go | 84 ++++++-- internal/groups/api/logging.go | 197 ++++++++++++++---- lora/api/logging.go | 147 +++++++++++--- opcua/api/logging.go | 148 +++++++++++--- pkg/messaging/handler/logging.go | 121 +++++++++--- provision/api/logging.go | 49 ++++- readers/api/logging.go | 17 +- things/api/logging.go | 255 +++++++++++++++++++----- twins/api/logging.go | 121 ++++++++++-- users/api/logging.go | 308 ++++++++++++++++++++++++----- ws/api/logging.go | 17 +- 14 files changed, 1490 insertions(+), 316 deletions(-) diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 43658a58e..557785950 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -30,12 +30,23 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method add using token %s with thing %s took %s to complete", token, saved.ThingID, time.Since(begin)) + message := "Method add completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "add"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "add"), + slog.String("thing_id", saved.ThingID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Add(ctx, token, cfg) @@ -45,12 +56,23 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view using token %s with thing %s took %s to complete", token, saved.ThingID, time.Since(begin)) + message := "Method view completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view"), + slog.String("thing_id", saved.ThingID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.View(ctx, token, id) @@ -60,12 +82,23 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update using token %s with thing %s took %s to complete", token, cfg.ThingID, time.Since(begin)) + message := "Method update completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update"), + slog.String("thing_id", cfg.ThingID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Update(ctx, token, cfg) @@ -75,12 +108,23 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_cert using token %s with thing id %s took %s to complete", token, thingID, time.Since(begin)) + message := "Method update_cert completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_cert"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_cert"), + slog.String("thing_id", thingID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateCert(ctx, token, thingID, clientCert, clientKey, caCert) @@ -90,12 +134,23 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_connections using token %s with thing %s took %s to complete", token, id, time.Since(begin)) + message := "Method update_connections completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update_connections"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_connections"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateConnections(ctx, token, id, connections) @@ -105,12 +160,24 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list using token %s with offset %d and limit %d took %s to complete", token, offset, limit, time.Since(begin)) + message := "Method list completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list"), + slog.String("token", token), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.List(ctx, token, filter, offset, limit) @@ -120,12 +187,23 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method remove using token %s with thing %s took %s to complete", token, id, time.Since(begin)) + message := "Method remove completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Remove(ctx, token, id) @@ -133,12 +211,23 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method bootstrap for thing with external id %s took %s to complete", externalID, time.Since(begin)) + message := "Method bootstrap completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "bootstrap"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "bootstrap"), + slog.String("external_id", externalID), + slog.String("external_key", externalKey), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Bootstrap(ctx, externalKey, externalID, secure) @@ -146,12 +235,24 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method change_state for token %s and thing %s took %s to complete", token, id, time.Since(begin)) + message := "Method change_state completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "change_state"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "change_state"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("state", state.String()), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ChangeState(ctx, token, id, state) @@ -159,12 +260,22 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_channel_handler for channel %s took %s to complete", channel.ID, time.Since(begin)) + message := "Method update_channel_handler completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_channel_handler"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_channel_handler"), + slog.String("channel_id", channel.ID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateChannelHandler(ctx, channel) @@ -172,12 +283,22 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method remove_config_handler for config %s took %s to complete", id, time.Since(begin)) + message := "Method remove_config_handler completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove_config_handler"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_config_handler"), + slog.String("config_id", id), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveConfigHandler(ctx, id) @@ -185,12 +306,22 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method remove_channel_handler for channel %s took %s to complete", id, time.Since(begin)) + message := "Method remove_channel_handler completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "remove_channel_handler"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_channel_handler"), + slog.String("channel_id", id), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveChannelHandler(ctx, id) @@ -198,12 +329,23 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disconnect_thing_handler for channel %s and thing %s took %s to complete", channelID, thingID, time.Since(begin)) + message := "Method disconnect_thing_handler completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "disconnect_thing_handler"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disconnect_thing_handler"), + slog.String("channel_id", channelID), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisconnectThingHandler(ctx, channelID, thingID) diff --git a/certs/api/logging.go b/certs/api/logging.go index 63eba824c..003dc5bea 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -30,12 +30,23 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method issue_cert using token %s and thing %s took %s to complete", token, thingID, time.Since(begin)) + message := "Method issue_cert completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "issue_cert"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info(fmt.Sprintf("%s without errors.", message), + slog.String("method", "issue_cert"), + slog.String("thing_id", thingID), + slog.String("token", token), + slog.String("ttl", ttl), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.IssueCert(ctx, token, thingID, ttl) @@ -44,12 +55,25 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl // ListCerts logs the list_certs request. It logs the token, thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_certs using token %s and thing id %s took %s to complete", token, thingID, time.Since(begin)) + message := "Method list_certs completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_certs"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_certs"), + slog.String("thing_id", thingID), + slog.String("token", token), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListCerts(ctx, token, thingID, offset, limit) @@ -74,12 +98,23 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_cert using token %s and serial id %s took %s to complete", token, serialID, time.Since(begin)) + message := "Method view_cert completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_cert"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_cert"), + slog.String("token", token), + slog.String("serial_id", serialID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewCert(ctx, token, serialID) @@ -89,12 +124,23 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method revoke_cert using token %s and thing %s took %s to complete", token, thingID, time.Since(begin)) + message := "Method revoke_cert completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "revoke_cert"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "revoke_cert"), + slog.String("token", token), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RevokeCert(ctx, token, thingID) diff --git a/coap/api/logging.go b/coap/api/logging.go index 6d0a61248..2a89cb93a 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -35,12 +35,22 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa if msg.GetSubtopic() != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, msg.GetSubtopic()) } - message := fmt.Sprintf("Method publish to %s took %s to complete", destChannel, time.Since(begin)) + message := "Method publish complete" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "publish"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "publish"), + slog.String("channel", destChannel), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Publish(ctx, key, msg) @@ -54,12 +64,24 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := fmt.Sprintf("Method subscribe to %s for client %s took %s to complete", destChannel, c.Token(), time.Since(begin)) + message := "Method subscribe completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "subscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "subscribe"), + slog.String("channel", destChannel), + slog.String("client", c.Token()), + slog.String("channelID", chanID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Subscribe(ctx, key, chanID, subtopic, c) @@ -73,12 +95,24 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := fmt.Sprintf("Method unsubscribe for the client %s from the channel %s took %s to complete", token, destChannel, time.Since(begin)) + message := "Method unsubscribe completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "unsubscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "unsubscribe"), + slog.String("channel", destChannel), + slog.String("token", token), + slog.String("key", key), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Unsubscribe(ctx, key, chanID, subtopic, token) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index e8e0ebba2..4d6435bc6 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -30,12 +30,23 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_subscription with the id %s for token %s took %s to complete", id, token, time.Since(begin)) + message := "Method create_subscription completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "create_subscription"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_subscription"), + slog.String("id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateSubscription(ctx, token, sub) @@ -45,12 +56,23 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_subscription with the topic %s for token %s took %s to complete", topic, token, time.Since(begin)) + message := "Method view_subscription completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_subscription"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_subscription"), + slog.String("topic", topic), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewSubscription(ctx, token, topic) @@ -60,12 +82,23 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_subscriptions for topic %s and token %s took %s to complete", pm.Topic, token, time.Since(begin)) + message := "Method list_subscriptions completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_subscriptions"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_subscriptions"), + slog.String("topic", pm.Topic), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListSubscriptions(ctx, token, pm) @@ -75,12 +108,23 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method remove_subscription for subscription %s took %s to complete", id, time.Since(begin)) + message := "Method remove_subscription completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "remove_subscription"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_subscription"), + slog.String("id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveSubscription(ctx, token, id) @@ -90,12 +134,22 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method consume took %s to complete", time.Since(begin)) + message := "Method consume completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "consume"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "consume"), + slog.String("message", fmt.Sprintf("%v", msg)), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ConsumeBlocking(ctx, msg) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 69fd42a1f..5991fbaac 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -28,12 +28,25 @@ func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_group for %s %s with id %s using token %s took %s to complete", g.Name, kind, g.ID, token, time.Since(begin)) + message := "Method create_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "create_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_group"), + slog.String("group_name", g.Name), + slog.String("token", token), + slog.String("kind", kind), + slog.String("group_id", g.ID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateGroup(ctx, token, kind, group) } @@ -42,12 +55,24 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_group for group %s with id %s using token %s took %s to complete", g.Name, g.ID, token, time.Since(begin)) + message := "Method update_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_group"), + slog.String("group_name", g.Name), + slog.String("group_id", g.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateGroup(ctx, token, group) } @@ -56,12 +81,24 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_group for group %s with id %s using token %s took %s to complete", g.Name, g.ID, token, time.Since(begin)) + message := "Method view_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_group"), + slog.String("group_name", g.Name), + slog.String("group_id", g.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewGroup(ctx, token, id) } @@ -70,12 +107,23 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_group_perms for group with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method view_group_perms completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_group_perms"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_group_perms"), + slog.String("group_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewGroupPerms(ctx, token, id) } @@ -84,12 +132,22 @@ func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, memberID string, gp groups.Page) (cg groups.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_groups %d groups using token %s took %s to complete", cg.Total, token, time.Since(begin)) + message := fmt.Sprintf("Method list_groups %d groups completed", cg.Total) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_groups"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_groups"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListGroups(ctx, token, memberKind, memberID, gp) } @@ -98,12 +156,24 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method enable_group for group with id %s using token %s took %s to complete", g.ID, token, time.Since(begin)) + message := "Method enable_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "enable_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "enable_group"), + slog.String("group_name", g.Name), + slog.String("group_id", g.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.EnableGroup(ctx, token, id) } @@ -112,12 +182,24 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disable_group for group with id %s using token %s took %s to complete", g.ID, token, time.Since(begin)) + message := "Method disable_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "disable_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disable_group"), + slog.String("group_name", g.Name), + slog.String("group_id", g.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisableGroup(ctx, token, id) } @@ -126,24 +208,51 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (mp groups.MembersPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_memberships for group with id %s using token %s took %s to complete", groupID, token, time.Since(begin)) + message := "Method list_memberships completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_memberships"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_memberships"), + slog.String("group_id", groupID), + slog.String("token", token), + slog.String("permission", permission), + slog.String("member_kind", memberKind), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListMembers(ctx, token, groupID, permission, memberKind) } func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method assign for token %s and member %s group id %s took %s to complete", token, memberIDs, groupID, time.Since(begin)) + message := "Method assign completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "assign"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "assign"), + slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), + slog.String("group_id", groupID), + slog.String("token", token), + slog.String("relation", relation), + slog.String("member_kind", memberKind), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Assign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -151,12 +260,24 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method unassign for token %s and member %s group id %s took %s to complete", token, memberIDs, groupID, time.Since(begin)) + message := "Method unassign completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "unassign"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "unassign"), + slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), + slog.String("group_id", groupID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Unassign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -164,12 +285,22 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method delete_group for group with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method delete_group completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "delete_group"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("group_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DeleteGroup(ctx, token, id) } diff --git a/lora/api/logging.go b/lora/api/logging.go index fb9a37cfe..9388e2c2a 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -31,12 +31,23 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("create_thing for thing %s and lora-dev-eui %s took %s to complete", thingID, loraDevEUI, time.Since(begin)) + message := "create_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "create_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_thing"), + slog.String("thing_id", thingID), + slog.String("lora_dev_eui", loraDevEUI), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateThing(ctx, thingID, loraDevEUI) @@ -44,12 +55,23 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("update_thing for thing %s and lora-dev-eui %s took %s to complete", thingID, loraDevEUI, time.Since(begin)) + message := "Method update_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_thing"), + slog.String("thing_id", thingID), + slog.String("lora_dev_eui", loraDevEUI), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateThing(ctx, thingID, loraDevEUI) @@ -57,12 +79,22 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("remove_thing for thing %s took %s to complete", thingID, time.Since(begin)) + message := "remove_thing complete" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_thing"), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveThing(ctx, thingID) @@ -70,12 +102,23 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("create_channel for channel %s and lora-app %s took %s to complete", chanID, loraApp, time.Since(begin)) + message := "Method create_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "create_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_channel"), + slog.String("channel_id", chanID), + slog.String("lora_app", loraApp), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateChannel(ctx, chanID, loraApp) @@ -83,12 +126,23 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("update_channel for channel %s and lora-app %s took %s to complete", chanID, loraApp, time.Since(begin)) + message := "Method update_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_channel"), + slog.String("channel_id", chanID), + slog.String("lora_app", loraApp), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateChannel(ctx, chanID, loraApp) @@ -96,12 +150,22 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("remove_channel for channel %s took %s to complete", chanID, time.Since(begin)) + message := "Method remove_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_channel"), + slog.String("channel_id", chanID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveChannel(ctx, chanID) @@ -109,12 +173,23 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("connect_thing for channel %s and thing %s, took %s to complete", chanID, thingID, time.Since(begin)) + message := "Method connect_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "connect_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "connect_thing"), + slog.String("channel_id", chanID), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ConnectThing(ctx, chanID, thingID) @@ -124,10 +199,21 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID defer func(begin time.Time) { message := fmt.Sprintf("disconnect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "disconnect_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disconnect_thing"), + slog.String("channel_id", chanID), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisconnectThing(ctx, chanID, thingID) @@ -137,10 +223,21 @@ func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err defer func(begin time.Time) { message := fmt.Sprintf("publish application/%s/device/%s/rx took %s to complete", msg.ApplicationID, msg.DevEUI, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "publish"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "publish"), + slog.String("application_id", msg.ApplicationID), + slog.String("device_eui", msg.DevEUI), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Publish(ctx, msg) diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 27eeb6ce0..6684ea431 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -31,12 +31,23 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("create_thing %s with NodeID %s, took %s to complete", mgxThing, opcuaNodeID, time.Since(begin)) + message := "Method create_thing complete" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "create_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_thing"), + slog.String("thing_id", mgxThing), + slog.String("opcua_node_id", opcuaNodeID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) @@ -44,12 +55,23 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("update_thing %s with NodeID %s, took %s to complete", mgxThing, opcuaNodeID, time.Since(begin)) + message := "Method update_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_thing"), + slog.String("thing_id", mgxThing), + slog.String("opcua_node_id", opcuaNodeID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateThing(ctx, mgxThing, opcuaNodeID) @@ -57,12 +79,22 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("remove_thing %s, took %s to complete", mgxThing, time.Since(begin)) + message := "Method remove_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_thing"), + slog.String("thing_id", mgxThing), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveThing(ctx, mgxThing) @@ -70,12 +102,23 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("create_channel %s with ServerURI %s, took %s to complete", mgxChan, opcuaServerURI, time.Since(begin)) + message := "create_channel complete" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "create_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_channel"), + slog.String("channel_id", mgxChan), + slog.String("opcua_server_uri", opcuaServerURI), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateChannel(ctx, mgxChan, opcuaServerURI) @@ -83,12 +126,23 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("update_channel %s with ServerURI %s, took %s to complete", mgxChanID, opcuaServerURI, time.Since(begin)) + message := "update_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "update_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_channel"), + slog.String("channel_id", mgxChanID), + slog.String("opcua_server_uri", opcuaServerURI), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateChannel(ctx, mgxChanID, opcuaServerURI) @@ -96,12 +150,22 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("remove_channel %s, took %s to complete", mgxChanID, time.Since(begin)) + message := "remove_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "remove_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_channel"), + slog.String("channel_id", mgxChanID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveChannel(ctx, mgxChanID) @@ -109,12 +173,23 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("connect_thing for channel %s and thing %s, took %s to complete", mgxChanID, mgxThingID, time.Since(begin)) + message := "Method connect_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "connect_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "connect_thing"), + slog.String("channel_id", mgxChanID), + slog.String("thing_id", mgxThingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ConnectThing(ctx, mgxChanID, mgxThingID) @@ -124,10 +199,21 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT defer func(begin time.Time) { message := fmt.Sprintf("disconnect_thing mgx-%s : mgx-%s, took %s to complete", mgxChanID, mgxThingID, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "disconnect_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disconnect_thing"), + slog.String("channel_id", mgxChanID), + slog.String("thing_id", mgxThingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisconnectThing(ctx, mgxChanID, mgxThingID) @@ -137,10 +223,22 @@ func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, id defer func(begin time.Time) { message := fmt.Sprintf("browse server URI %s and node %s;%s, took %s to complete", serverURI, namespace, identifier, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "browse"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "browse"), + slog.String("server_uri", serverURI), + slog.String("namespace", namespace), + slog.String("identifier", identifier), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Browse(ctx, serverURI, namespace, identifier) diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 5019c20b4..bb3b2b2f3 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -24,12 +24,21 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method auth connect took %s to complete", time.Since(begin)) + message := "Method auth connect completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "auth_connect"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "auth_connect"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AuthConnect(ctx) @@ -38,12 +47,22 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method auth publish took %s to complete", time.Since(begin)) + message := "Method auth publish completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "auth_publish"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "auth_publish"), + slog.String("topic", *topic), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AuthPublish(ctx, topic, payload) @@ -52,12 +71,21 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method auth subscribe took %s to complete", time.Since(begin)) + message := "Method auth subscribe completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "auth_subscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "auth_subscribe"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AuthSubscribe(ctx, topics) @@ -66,12 +94,21 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method connect took %s to complete", time.Since(begin)) + message := "Method connect completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "connect"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "connect"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Connect(ctx) @@ -80,12 +117,21 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disconnect took %s to complete", time.Since(begin)) + message := "Method disconnect completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "disconnect"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disconnect"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Disconnect(ctx) @@ -95,12 +141,22 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method publish to channel %s took %s to complete", *topic, time.Since(begin)) + message := "Method publish completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "publish"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "publish"), + slog.String("channel_topic", *topic), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Publish(ctx, topic, payload) @@ -109,12 +165,21 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method subscribe took %s to complete", time.Since(begin)) + message := "Method subscribe completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "subscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "subscribe"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Subscribe(ctx, topics) @@ -125,10 +190,20 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) defer func(begin time.Time) { message := fmt.Sprintf("Method unsubscribe took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "unsubscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "unsubscribe"), + slog.String("topics", fmt.Sprintf("%v", topics)), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Unsubscribe(ctx, topics) diff --git a/provision/api/logging.go b/provision/api/logging.go index f1dfd948e..6d9f69fab 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -27,12 +27,22 @@ func NewLoggingMiddleware(svc provision.Service, logger *slog.Logger) provision. func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey string) (res provision.Result, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method provision for token: %s and things: %v took %s to complete", token, res.Things, time.Since(begin)) + message := fmt.Sprintf("Method provision for things: %v took completed", res.Things) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "provision"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors", message), + slog.String("method", "provision"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Provision(token, name, externalID, externalKey) @@ -40,12 +50,23 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method cert for token: %s and thing: %v took %s to complete", token, thingID, time.Since(begin)) + message := "Method cert complete completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s", message, err), + slog.String("method", "cert"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors", message), + slog.String("method", "cert"), + slog.String("token", token), + slog.String("thing_id", thingID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Cert(token, thingID, duration) @@ -53,12 +74,22 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method mapping for token: %s took %s to complete", token, time.Since(begin)) + message := "Method mapping completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s", message, err), + slog.String("method", "mapping"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors", message), + slog.String("method", "mapping"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Mapping(token) diff --git a/readers/api/logging.go b/readers/api/logging.go index 73bb9dcff..94fa6b582 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -30,12 +30,23 @@ func LoggingMiddleware(svc readers.MessageRepository, logger *slog.Logger) reade func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (page readers.MessagesPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method read_all for channel %s with query %v took %s to complete", chanID, rpm, time.Since(begin)) + message := "Method read_all completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "read_all"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "read_all"), + slog.String("channel_ID", chanID), + slog.String("query", fmt.Sprintf("%v", rpm)), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ReadAll(chanID, rpm) diff --git a/things/api/logging.go b/things/api/logging.go index 134c8eea8..68b792145 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -27,180 +27,345 @@ func LoggingMiddleware(svc things.Service, logger *slog.Logger) things.Service { func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, clients ...mgclients.Client) (cs []mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_things %d things using token %s took %s to complete", len(cs), token, time.Since(begin)) + message := fmt.Sprintf("Method create_things %d things completed", len(cs)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("mathod", "create_things"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("mathod", "create_things"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateThings(ctx, token, clients...) } func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_thing for thing with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method view_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_thing"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_thing_permissions for thing with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method view_thing_permissions completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_thing_permissions"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_thing_permissions"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewClientPerms(ctx, token, id) } func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_things using token %s took %s to complete", token, time.Since(begin)) + message := "Method list_things completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_things"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_things"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListClients(ctx, token, reqUserID, pm) } func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_thing_name_and_metadata for thing with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_thing_name_and_metadata completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_thing_name_and_metadata"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_thing_name_and_metadata"), + slog.String("thing_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_thing_tags for thing with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_thing_tags completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_thing_tags"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_thing_tags"), + slog.String("thing_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_thing_secret for thing with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_thing_secret completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "update_thing_secret"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_thing_secret"), + slog.String("thing_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method enable_thing for thing with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method enable_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with errors.", message), + slog.String("method", "enable_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "enable_thing"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disable_thing for thing with id %s using token %s took %s to complete", id, token, time.Since(begin)) + message := "Method disable_thing completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "disable_thing"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disable_thing"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, channelID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_things_by_channel for channel with id %s using token %s took %s to complete", channelID, token, time.Since(begin)) + message := "Method list_things_by_channel completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_things_by_channel"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_things_by_channel"), + slog.String("channel_id", channelID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListClientsByGroup(ctx, token, channelID, cp) } func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method identify for thing with id %s and key %s took %s to complete", id, key, time.Since(begin)) + message := "Method identify completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "identify"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "identify"), + slog.String("thing_id", id), + slog.String("thing_key", key), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Identify(ctx, key) } func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (id string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method authorize for thing key %s and channnel %s took %s to complete", req.Subject, req.Object, time.Since(begin)) + message := "Method authorise completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "authorise"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "authorize"), + slog.String("thing_key", req.Subject), + slog.String("channel_id", req.Object), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Authorize(ctx, req) } func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method share for thing id %s with relation %s for users %v took %s to complete", id, relation, userids, time.Since(begin)) + message := fmt.Sprintf("Method share for users %v completex", userids) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "share"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "share"), + slog.String("thing_id", id), + slog.String("relation", relation), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Share(ctx, token, id, relation, userids...) } func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method unshare for thing id %s with relation %s for users %v took %s to complete", id, relation, userids, time.Since(begin)) + message := fmt.Sprintf("Method unshare for users %v completed", userids) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "unshare"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "unshare"), + slog.String("thing_id", id), + slog.String("relation", relation), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Unshare(ctx, token, id, relation, userids...) } func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method delete_client for thing id %s took %s to complete", id, time.Since(begin)) + message := "Method delete_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "delete_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "delete_client"), + slog.String("thing_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DeleteClient(ctx, token, id) } diff --git a/twins/api/logging.go b/twins/api/logging.go index c8b65305d..9ece6bd01 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -29,12 +29,24 @@ func LoggingMiddleware(svc twins.Service, logger *slog.Logger) twins.Service { func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (tw twins.Twin, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method add_twin for token %s and twin %s took %s to complete", token, twin.ID, time.Since(begin)) + message := "Method add_twin completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "add_twin"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "add_twin"), + slog.String("id", twin.ID), + slog.String("token", token), + slog.String("definition", fmt.Sprintf("%v", def)), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AddTwin(ctx, token, twin, def) @@ -42,12 +54,24 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_twin for token %s and twin %s took %s to complete", token, twin.ID, time.Since(begin)) + message := "Method update_twin completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_twin"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_twin"), + slog.String("id", twin.ID), + slog.String("token", token), + slog.String("definition", fmt.Sprintf("%v", def)), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateTwin(ctx, token, twin, def) @@ -55,12 +79,23 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) (tw twins.Twin, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_twin for token %s and twin %s took %s to complete", token, twinID, time.Since(begin)) + message := "Method view_twin completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "view_twin"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_twin"), + slog.String("id", twinID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewTwin(ctx, token, twinID) @@ -68,12 +103,24 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset, limit uint64, name string, metadata twins.Metadata) (page twins.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_twins for token %s took %s to complete", token, time.Since(begin)) + message := "Method list_twins completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_twins"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_twins"), + slog.String("token", token), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListTwins(ctx, token, offset, limit, name, metadata) @@ -81,12 +128,21 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Message) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method save_states took %s to complete", time.Since(begin)) + message := "Method save_states completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "save_states"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "save_states"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.SaveStates(ctx, msg) @@ -94,12 +150,24 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offset, limit uint64, twinID string) (page twins.StatesPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_states for token %s took %s to complete", token, time.Since(begin)) + message := "Method list_states completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_states"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_states"), + slog.String("token", token), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListStates(ctx, token, offset, limit, twinID) @@ -107,12 +175,23 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method remove_twin for token %s and twin %s took %s to complete", token, twinID, time.Since(begin)) + message := "Method remove_twin completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "remove_twin"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "remove_twin"), + slog.String("token", token), + slog.String("twin_ID", twinID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RemoveTwin(ctx, token, twinID) diff --git a/users/api/logging.go b/users/api/logging.go index ac60fea54..77e53be0e 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -30,12 +30,23 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method register_client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method register_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error: %s.", message, err), + slog.String("method", "register_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "register_client"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RegisterClient(ctx, token, client) } @@ -48,12 +59,23 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d if t != nil { message = fmt.Sprintf("%s of type %s", message, t.AccessType) } - message = fmt.Sprintf("%s for client %s took %s to complete", message, identity, time.Since(begin)) + message = fmt.Sprintf("%s completed", message) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "issue_token"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "issue_token"), + slog.String("identity", identity), + slog.String("domain_id", domainID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.IssueToken(ctx, identity, secret, domainID) } @@ -66,12 +88,23 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom if t != nil { message = fmt.Sprintf("%s of type %s", message, t.AccessType) } - message = fmt.Sprintf("%s for refresh token %s took %s to complete", message, refreshToken, time.Since(begin)) + message = fmt.Sprintf("%s completed", message) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "refresh_token"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "refresh_token"), + slog.String("refresh_token", refreshToken), + slog.String("domain_id", domainID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RefreshToken(ctx, refreshToken, domainID) } @@ -80,12 +113,23 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method view_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "view_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_client"), + slog.String("client_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } @@ -94,12 +138,22 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_profile with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method view_profile completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "view_profile"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "view_profile"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ViewProfile(ctx, token) } @@ -108,12 +162,22 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_clients %d clients using token %s took %s to complete", cp.Total, token, time.Since(begin)) + message := "Method list_clients completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "list_clients"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_clients"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListClients(ctx, token, pm) } @@ -122,12 +186,23 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_client_name_and_metadata for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error", message), + slog.String("method", "update_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_client"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } @@ -136,12 +211,23 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_client_tags for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_client_tags completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_client_tags"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_client_tags"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } @@ -150,12 +236,24 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_client_identity for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_client_identity completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_client_identity"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_client_identity"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("identity", identity), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientIdentity(ctx, token, id, identity) } @@ -164,12 +262,23 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_client_secret for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_client_secret completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_client_secret"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_client_secret"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } @@ -178,12 +287,23 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method generate_reset_token for email %s and host %s took %s to complete", email, host, time.Since(begin)) + message := "Method generate_reset_token completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "generate_reset_token"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "generate_reset_token"), + slog.String("email", email), + slog.String("host", host), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.GenerateResetToken(ctx, email, host) } @@ -192,12 +312,22 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method reset_secret using token %s took %s to complete", token, time.Since(begin)) + message := "Method reset_secret completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "reset_secret"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "reset_secret"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ResetSecret(ctx, token, secret) } @@ -206,12 +336,25 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method send_password_reset using token %s took %s to complete", token, time.Since(begin)) + message := "Method send_password_reset completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "send_password_reset"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "send_password_reset"), + slog.String("host", host), + slog.String("email", email), + slog.String("user", user), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.SendPasswordReset(ctx, host, email, user, token) } @@ -220,12 +363,24 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_client_role for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method update_client_role completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_client_role"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_client_role"), + slog.String("client_id", c.ID), + slog.String("role", client.Role.String()), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateClientRole(ctx, token, client) } @@ -234,12 +389,23 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method enable_client for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method enable_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "enable_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "enable_client"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } @@ -248,12 +414,23 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disable_client for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) + message := "Method disable_client completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "disable_client"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "disable_client"), + slog.String("client_id", c.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } @@ -262,12 +439,24 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_members %d members for object kind %s and object id %s and token %s took %s to complete", mp.Total, objectKind, objectID, token, time.Since(begin)) + message := fmt.Sprintf("Method list_members %d members completed", mp.Total) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_members"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_members"), + slog.String("object_kind", objectKind), + slog.String("object_id", objectID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListMembers(ctx, token, objectKind, objectID, cp) } @@ -275,12 +464,23 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, // Identify logs the identify request. It logs the token and the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method identify for token %s with id %s took %s to complete", token, id, time.Since(begin)) + message := "Method identify completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "identify"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "identify"), + slog.String("client_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Identify(ctx, token) } diff --git a/ws/api/logging.go b/ws/api/logging.go index c04e93250..6e2af7fe6 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -32,12 +32,23 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, su if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := fmt.Sprintf("Method subscribe to channel %s took %s to complete", destChannel, time.Since(begin)) + message := "Method subscribe completed" if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "subscribe"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "subscribe"), + slog.String("channel", destChannel), + slog.String("thing_key", thingKey), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Subscribe(ctx, thingKey, chanID, subtopic, c) From f81d6125b104d05147b2af920a739497d9726b84 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 15 Jan 2024 18:38:36 +0300 Subject: [PATCH 02/23] fix: remove repetition Signed-off-by: Musilah --- auth/api/logging.go | 374 +++++++++++++++++++++++------ bootstrap/api/logging.go | 15 +- certs/api/logging.go | 24 +- coap/api/logging.go | 17 +- consumers/notifiers/api/logging.go | 7 +- internal/groups/api/logging.go | 12 +- lora/api/logging.go | 31 +-- opcua/api/logging.go | 31 +-- pkg/messaging/handler/logging.go | 9 +- provision/api/logging.go | 4 +- readers/api/logging.go | 2 +- things/api/logging.go | 18 +- twins/api/logging.go | 9 +- users/api/logging.go | 66 ++--- ws/api/logging.go | 2 +- 15 files changed, 393 insertions(+), 228 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index f73b9b05f..547ef4cf6 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/auth" ) +const message = "Method completed" + var _ auth.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -28,12 +30,20 @@ func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service { func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_objects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_objects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_objects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListObjects(ctx, pr, nextPageToken, limit) @@ -41,12 +51,20 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_all_objects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_all_objects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_all_objects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListAllObjects(ctx, pr) @@ -54,24 +72,40 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method count_objects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "count_objects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "count_objects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CountObjects(ctx, pr) } func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_subjects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_subjects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_subjects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListSubjects(ctx, pr, nextPageToken, limit) @@ -79,12 +113,20 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_all_subjects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_all_subjects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_all_subjects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListAllSubjects(ctx, pr) @@ -92,24 +134,40 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_subjects took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_subjects"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_subjects"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CountSubjects(ctx, pr) } func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.PolicyReq, filterPermissions []string) (p auth.Permissions, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_permissions took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_permissions"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_permissions"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListPermissions(ctx, pr, filterPermissions) @@ -121,12 +179,24 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K if key.Type != auth.AccessKey && !key.ExpiresAt.IsZero() { d = fmt.Sprintf("with expiration date %v", key.ExpiresAt) } - message := fmt.Sprintf("Method issue for %d key %s took %s to complete", key.Type, d, time.Since(begin)) + message := fmt.Sprintf("Method issue for key %s completed", d) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "issue"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "issue"), + slog.String("key_type", key.Type.String()), + slog.String("key_id", key.ID), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Issue(ctx, token, key) @@ -134,12 +204,22 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method revoke for key %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "revoke"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "revoke"), + slog.String("key_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Revoke(ctx, token, id) @@ -147,12 +227,22 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) (key auth.Key, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method retrieve for key %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "retrieve_key"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "retrieve_key"), + slog.String("key_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RetrieveKey(ctx, token, id) @@ -160,12 +250,21 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id auth.Key, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method identify took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "identify"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "identify"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Identify(ctx, token) @@ -173,36 +272,60 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method authorize took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "authorize"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "authorize"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.Authorize(ctx, pr) } func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method add_policy took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "add_policy"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "add_policy"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AddPolicy(ctx, pr) } func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_policy_bulk took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "create_policy_bulk"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_policy_bulk"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AddPolicies(ctx, prs) @@ -210,132 +333,233 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method delete_policy took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "delete_policy"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "delete_policy"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DeletePolicy(ctx, pr) } func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method delete_policies took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "delete_policies"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "delete_policies"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.DeletePolicies(ctx, prs) } func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d auth.Domain) (do auth.Domain, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_domain took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "create_domain"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "create_domain"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.CreateDomain(ctx, token, d) } func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id string) (do auth.Domain, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method retrieve_domain for domain id %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "retrieve_domain"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "retrieve_domain"), + slog.String("domain_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RetrieveDomain(ctx, token, id) } func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, token, id string) (permissions auth.Permissions, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method retrieve_domain_permissions for domain id %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "retrieve_domain_permissions"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "retrieve_domain_permissions"), + slog.String("domain_id", id), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.RetrieveDomainPermissions(ctx, token, id) } func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method update_domain for domain id %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "update_domain"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "update_domain"), + slog.String("domain_id", id), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UpdateDomain(ctx, token, id, d) } func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method change_domain_status for domain id %s took %s to complete", id, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "change_domain_status"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "change_domain_status"), + slog.String("domain_id", "id"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ChangeDomainStatus(ctx, token, id, d) } func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_domains took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_domains"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_domains"), + slog.String("token", token), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListDomains(ctx, token, page) } func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method assign_users took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "assign_users"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "assign_users"), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.AssignUsers(ctx, token, id, userIds, relation) } func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method unassign_users took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "unassign_users"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "unassign_users"), + slog.String("token", token), + slog.String("user_ids", fmt.Sprintf("%v", userIds)), + slog.String("relation", relation), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.UnassignUsers(ctx, token, id, userIds, relation) } func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_user_domains took %s to complete", time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_user_domains"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_user_domains"), + slog.String("token", token), + slog.String("user_id", userID), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListUserDomains(ctx, token, userID, page) } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 557785950..cf4eb7f00 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/bootstrap" ) +const message = "Method completed" + var _ bootstrap.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,7 +32,6 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method add completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -56,7 +57,6 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method view completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -82,7 +82,6 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { - message := "Method update completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -108,7 +107,6 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method update_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -134,7 +132,6 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { - message := "Method update_connections completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -160,7 +157,6 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { - message := "Method list completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -187,7 +183,6 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method remove completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -211,7 +206,6 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method bootstrap completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -235,7 +229,6 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { - message := "Method change_state completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -260,7 +253,6 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { - message := "Method update_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -283,7 +275,6 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_config_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -306,7 +297,6 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -329,7 +319,6 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { - message := "Method disconnect_thing_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/certs/api/logging.go b/certs/api/logging.go index 003dc5bea..e10e2c6c5 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/certs" ) +const message = "Method completed" + var _ certs.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,7 +32,6 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := "Method issue_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -55,7 +56,6 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl // ListCerts logs the list_certs request. It logs the token, thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message := "Method list_certs completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -83,12 +83,24 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_serials using token %s and thing id %s took %s to complete", token, thingID, time.Since(begin)) if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + lm.logger.Warn( + fmt.Sprintf("%s with error.", message), + slog.String("method", "list_serials"), + slog.String("error", err.Error()), + slog.String("duration", time.Since(begin).String()), + ) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info( + fmt.Sprintf("%s without errors.", message), + slog.String("method", "list_serials"), + slog.String("token", token), + slog.String("thing_id", thingID), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + ) }(time.Now()) return lm.svc.ListSerials(ctx, token, thingID, offset, limit) @@ -98,7 +110,6 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := "Method view_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -124,7 +135,6 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { - message := "Method revoke_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/coap/api/logging.go b/coap/api/logging.go index 2a89cb93a..8eb250b6b 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -15,6 +15,8 @@ import ( "github.com/absmach/magistrala/pkg/messaging" ) +const logmessage = "Method completed" + var _ coap.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -35,10 +37,9 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa if msg.GetSubtopic() != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, msg.GetSubtopic()) } - message := "Method publish complete" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), + fmt.Sprintf("%s with error.", logmessage), slog.String("method", "publish"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -46,7 +47,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), + fmt.Sprintf("%s without errors.", logmessage), slog.String("method", "publish"), slog.String("channel", destChannel), slog.String("duration", time.Since(begin).String()), @@ -64,10 +65,9 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := "Method subscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), + fmt.Sprintf("%s with error.", logmessage), slog.String("method", "subscribe"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -75,7 +75,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), + fmt.Sprintf("%s without errors.", logmessage), slog.String("method", "subscribe"), slog.String("channel", destChannel), slog.String("client", c.Token()), @@ -95,10 +95,9 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := "Method unsubscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error: %s.", logmessage, err), slog.String("method", "unsubscribe"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -106,7 +105,7 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), + fmt.Sprintf("%s without errors.", logmessage), slog.String("method", "unsubscribe"), slog.String("channel", destChannel), slog.String("token", token), diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 4d6435bc6..395a29cb4 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/consumers/notifiers" ) +const message = "Method completed" + var _ notifiers.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,7 +32,6 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { - message := "Method create_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -56,7 +57,6 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { - message := "Method view_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -82,7 +82,6 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { - message := "Method list_subscriptions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -108,7 +107,6 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -134,7 +132,6 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { - message := "Method consume completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 5991fbaac..e83a767a1 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -12,6 +12,8 @@ import ( "github.com/absmach/magistrala/pkg/groups" ) +const message = "Method completed" + var _ groups.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -28,7 +30,6 @@ func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method create_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -55,7 +56,6 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method update_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -81,7 +81,6 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method view_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -107,7 +106,6 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - message := "Method view_group_perms completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -156,7 +154,6 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method enable_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -182,7 +179,6 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method disable_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -208,7 +204,6 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (mp groups.MembersPage, err error) { defer func(begin time.Time) { - message := "Method list_memberships completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -233,7 +228,6 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := "Method assign completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -260,7 +254,6 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := "Method unassign completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -285,7 +278,6 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method delete_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/lora/api/logging.go b/lora/api/logging.go index 9388e2c2a..437e91afe 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/lora" ) +const message = "Method completed" + var _ lora.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -31,10 +33,9 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := "create_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "create_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -55,10 +56,9 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := "Method update_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "update_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -79,10 +79,9 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { - message := "remove_thing complete" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "remove_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -102,10 +101,9 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := "Method create_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "create_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -126,10 +124,9 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := "Method update_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "update_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -150,10 +147,9 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { - message := "Method remove_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "remove_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -173,10 +169,9 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - message := "Method connect_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "connect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -197,10 +192,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("disconnect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) + message := fmt.Sprintf("Method disconnect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "disconnect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -221,10 +216,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("publish application/%s/device/%s/rx took %s to complete", msg.ApplicationID, msg.DevEUI, time.Since(begin)) + message := fmt.Sprintf("Method publish application/%s/device/%s/rx took %s to complete", msg.ApplicationID, msg.DevEUI, time.Since(begin)) if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "publish"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 6684ea431..ea0157ba1 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/opcua" ) +const message = "Method completed" + var _ opcua.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -31,10 +33,9 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := "Method create_thing complete" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "create_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -55,10 +56,9 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := "Method update_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "update_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -79,10 +79,9 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { - message := "Method remove_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "remove_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -102,10 +101,9 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := "create_channel complete" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "create_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -126,10 +124,9 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := "update_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "update_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -150,10 +147,9 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { - message := "remove_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "remove_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -173,10 +169,9 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := "Method connect_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "connect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -197,10 +192,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("disconnect_thing mgx-%s : mgx-%s, took %s to complete", mgxChanID, mgxThingID, time.Since(begin)) + message := fmt.Sprintf("Method disconnect_thing mgx-%s : mgx-%s, took %s to complete", mgxChanID, mgxThingID, time.Since(begin)) if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "disconnect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -221,10 +216,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, identifier string) (nodes []opcua.BrowsedNode, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("browse server URI %s and node %s;%s, took %s to complete", serverURI, namespace, identifier, time.Since(begin)) + message := fmt.Sprintf("Method browse server URI %s and node %s;%s, took %s to complete", serverURI, namespace, identifier, time.Since(begin)) if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "browse"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index bb3b2b2f3..53d068ed6 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/mproxy/pkg/session" ) +const message = "Method completed" + var _ session.Handler = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -24,7 +26,6 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method auth connect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -47,7 +48,6 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := "Method auth publish completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -71,7 +71,6 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := "Method auth subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -94,7 +93,6 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method connect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -117,7 +115,6 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method disconnect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -141,7 +138,6 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := "Method publish completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -165,7 +161,6 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := "Method subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), diff --git a/provision/api/logging.go b/provision/api/logging.go index 6d9f69fab..792c58ae7 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -50,7 +50,7 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key string, err error) { defer func(begin time.Time) { - message := "Method cert complete completed" + message := "Method completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s", message, err), @@ -74,7 +74,7 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, err error) { defer func(begin time.Time) { - message := "Method mapping completed" + message := "Method completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s", message, err), diff --git a/readers/api/logging.go b/readers/api/logging.go index 94fa6b582..4afe8d966 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc readers.MessageRepository, logger *slog.Logger) reade func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (page readers.MessagesPage, err error) { defer func(begin time.Time) { - message := "Method read_all completed" + message := "Method completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/things/api/logging.go b/things/api/logging.go index 68b792145..91dde3743 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/things" ) +const message = "Method completed" + var _ things.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -49,7 +51,6 @@ func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, cli func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method view_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -72,7 +73,6 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - message := "Method view_thing_permissions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -95,7 +95,6 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - message := "Method list_things completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -117,7 +116,6 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_thing_name_and_metadata completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -140,7 +138,6 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_thing_tags completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -163,7 +160,6 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_thing_secret completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -186,7 +182,6 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method enable_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with errors.", message), @@ -209,7 +204,6 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method disable_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -232,7 +226,6 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, channelID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - message := "Method list_things_by_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -255,7 +248,6 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id string, err error) { defer func(begin time.Time) { - message := "Method identify completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -278,7 +270,6 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (id string, err error) { defer func(begin time.Time) { - message := "Method authorise completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -301,7 +292,6 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method share for users %v completex", userids) if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -315,6 +305,7 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri fmt.Sprintf("%s without errors.", message), slog.String("method", "share"), slog.String("thing_id", id), + slog.String("user_IDs", fmt.Sprintf("%v", userids)), slog.String("relation", relation), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -325,7 +316,6 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method unshare for users %v completed", userids) if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -339,6 +329,7 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st fmt.Sprintf("%s without errors.", message), slog.String("method", "unshare"), slog.String("thing_id", id), + slog.String("user_IDs", fmt.Sprintf("%v", userids)), slog.String("relation", relation), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -349,7 +340,6 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method delete_client completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), diff --git a/twins/api/logging.go b/twins/api/logging.go index 9ece6bd01..711320835 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -15,6 +15,8 @@ import ( "github.com/absmach/magistrala/twins" ) +const message = "Method completed" + var _ twins.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -29,7 +31,6 @@ func LoggingMiddleware(svc twins.Service, logger *slog.Logger) twins.Service { func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (tw twins.Twin, err error) { defer func(begin time.Time) { - message := "Method add_twin completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -54,7 +55,6 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (err error) { defer func(begin time.Time) { - message := "Method update_twin completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -79,7 +79,6 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) (tw twins.Twin, err error) { defer func(begin time.Time) { - message := "Method view_twin completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -103,7 +102,6 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset, limit uint64, name string, metadata twins.Metadata) (page twins.Page, err error) { defer func(begin time.Time) { - message := "Method list_twins completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -128,7 +126,6 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Message) (err error) { defer func(begin time.Time) { - message := "Method save_states completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -150,7 +147,6 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offset, limit uint64, twinID string) (page twins.StatesPage, err error) { defer func(begin time.Time) { - message := "Method list_states completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -175,7 +171,6 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID string) (err error) { defer func(begin time.Time) { - message := "Method remove_twin completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/users/api/logging.go b/users/api/logging.go index 77e53be0e..1fc8ce345 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -14,6 +14,8 @@ import ( "github.com/absmach/magistrala/users" ) +const message = "Method completed" + var _ users.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,10 +32,9 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method register_client completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), + fmt.Sprintf("%s with error.", message), slog.String("method", "register_client"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -55,11 +56,6 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - message := "Method issue_token" - if t != nil { - message = fmt.Sprintf("%s of type %s", message, t.AccessType) - } - message = fmt.Sprintf("%s completed", message) if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -69,13 +65,17 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d ) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "issue_token"), - slog.String("identity", identity), - slog.String("domain_id", domainID), - slog.String("duration", time.Since(begin).String()), - ) + message := fmt.Sprintf("%s without errors.", message) + args := []interface{}{ + "method", "issue_token", + "identity", identity, + "domain_id", domainID, + "duration", time.Since(begin).String(), + } + if t.AccessType != "" { + args = append(args, "access_type", t.AccessType) + } + lm.logger.Info(message, args...) }(time.Now()) return lm.svc.IssueToken(ctx, identity, secret, domainID) } @@ -84,11 +84,6 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d // If the request fails, it logs the error. func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - message := "Method refresh_token" - if t != nil { - message = fmt.Sprintf("%s of type %s", message, t.AccessType) - } - message = fmt.Sprintf("%s completed", message) if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -98,13 +93,16 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom ) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "refresh_token"), - slog.String("refresh_token", refreshToken), - slog.String("domain_id", domainID), - slog.String("duration", time.Since(begin).String()), - ) + message := fmt.Sprintf("%s without errors.", message) + args := []interface{}{ + "method", "refresh_token", + "domain_id", domainID, + "duration", time.Since(begin).String(), + } + if t.AccessType != "" { + args = append(args, "access_type", t.AccessType) + } + lm.logger.Info(message, args...) }(time.Now()) return lm.svc.RefreshToken(ctx, refreshToken, domainID) } @@ -113,7 +111,6 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method view_client completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -138,7 +135,6 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method view_profile completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -162,7 +158,6 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - message := "Method list_clients completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -186,7 +181,6 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_client completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -211,7 +205,6 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_client_tags completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -236,7 +229,6 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_client_identity completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -262,7 +254,6 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_client_secret completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -287,7 +278,6 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { - message := "Method generate_reset_token completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -312,7 +302,6 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { - message := "Method reset_secret completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -336,7 +325,6 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { - message := "Method send_password_reset completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -363,7 +351,6 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method update_client_role completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -389,7 +376,6 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method enable_client completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -414,7 +400,6 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - message := "Method disable_client completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -464,13 +449,12 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, // Identify logs the identify request. It logs the token and the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { - message := "Method identify completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), slog.String("method", "identify"), slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), + slog.String("duration", time.Since(begin).String()), ) return } diff --git a/ws/api/logging.go b/ws/api/logging.go index 6e2af7fe6..8084b725b 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -32,7 +32,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, su if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message := "Method subscribe completed" + message := "Method completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), From 860b41e1c115e5ee9bef4d0513090b2f533b5153 Mon Sep 17 00:00:00 2001 From: Musilah Date: Wed, 17 Jan 2024 17:15:41 +0300 Subject: [PATCH 03/23] restructure slog attributes in users svc Signed-off-by: Musilah --- auth/api/logging.go | 76 ++---- bootstrap/api/logging.go | 43 ++-- certs/api/logging.go | 17 +- coap/api/logging.go | 17 +- consumers/notifiers/api/logging.go | 17 +- internal/groups/api/logging.go | 33 +-- lora/api/logging.go | 27 +-- opcua/api/logging.go | 67 +++--- pkg/messaging/handler/logging.go | 49 ++-- provision/api/logging.go | 14 +- readers/api/logging.go | 4 +- users/api/logging.go | 370 ++++++++++------------------- 12 files changed, 253 insertions(+), 481 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index 547ef4cf6..e3b659fd0 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/auth" ) -const message = "Method completed" - var _ auth.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,10 +28,10 @@ func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service { func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { + message := "Method list_objects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_objects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -41,7 +39,6 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_objects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -51,10 +48,10 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { + message := "Method list_all_objects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_all_objects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -62,7 +59,6 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_all_objects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -72,10 +68,10 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { + message := "Method count_objects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "count_objects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -83,7 +79,6 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "count_objects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -92,10 +87,10 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { + message := "Method list_subjects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_subjects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -103,7 +98,6 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_subjects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -113,10 +107,10 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { + message := "Method list_all_subjects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_all_subjects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -124,7 +118,6 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_all_subjects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -134,10 +127,10 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { + message := "Method count_subjects completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_subjects"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -145,7 +138,6 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_subjects"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -154,10 +146,10 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.PolicyReq, filterPermissions []string) (p auth.Permissions, err error) { defer func(begin time.Time) { + message := "Method list_permissions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_permissions"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -165,7 +157,6 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_permissions"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -183,7 +174,6 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "issue"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -191,7 +181,6 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "issue"), slog.String("key_type", key.Type.String()), slog.String("key_id", key.ID), slog.String("token", token), @@ -204,10 +193,10 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { + message := "Method revoke completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "revoke"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -215,7 +204,6 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "revoke"), slog.String("key_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -227,10 +215,10 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) (key auth.Key, err error) { defer func(begin time.Time) { + message := "Method retrieve_key completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "retrieve_key"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -238,7 +226,6 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "retrieve_key"), slog.String("key_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -250,10 +237,10 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id auth.Key, err error) { defer func(begin time.Time) { + message := "Method identify completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "identify"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -261,7 +248,6 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "identify"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), ) @@ -272,10 +258,10 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { + message := "Method authorize completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "authorize"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -283,7 +269,6 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "authorize"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -292,10 +277,10 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { + message := "Method add_policy completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "add_policy"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -303,7 +288,6 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "add_policy"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -312,10 +296,10 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { + message := "Method add_policies completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_policy_bulk"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -323,7 +307,6 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_policy_bulk"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -333,10 +316,10 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { + message := "Method delete_policy completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "delete_policy"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -344,7 +327,6 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "delete_policy"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -353,10 +335,10 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { + message := "Method delete_policies completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "delete_policies"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -364,7 +346,6 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "delete_policies"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -373,10 +354,10 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d auth.Domain) (do auth.Domain, err error) { defer func(begin time.Time) { + message := "Method create_domain completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_domain"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -384,7 +365,6 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_domain"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -393,10 +373,10 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id string) (do auth.Domain, err error) { defer func(begin time.Time) { + message := "Method retrieve_domain completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "retrieve_domain"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -404,7 +384,6 @@ func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id strin } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "retrieve_domain"), slog.String("domain_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -415,10 +394,10 @@ func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id strin func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, token, id string) (permissions auth.Permissions, err error) { defer func(begin time.Time) { + message := "Method retrieve_domain_permissions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "retrieve_domain_permissions"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -426,7 +405,6 @@ func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, toke } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "retrieve_domain_permissions"), slog.String("domain_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -437,10 +415,10 @@ func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, toke func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { + message := "Method update_domain completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_domain"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -448,7 +426,6 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_domain"), slog.String("domain_id", id), slog.String("duration", time.Since(begin).String()), ) @@ -458,10 +435,10 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { + message := "Method change_domain_status completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "change_domain_status"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -469,7 +446,6 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "change_domain_status"), slog.String("domain_id", "id"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -480,10 +456,10 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { + message := "Method list_domains completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_domains"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -491,7 +467,6 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_domains"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), ) @@ -501,10 +476,10 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { + message := "Method assign_users completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "assign_users"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -512,7 +487,6 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "assign_users"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -521,10 +495,10 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { + message := "Method unassign_users completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "unassign_users"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -532,7 +506,6 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "unassign_users"), slog.String("token", token), slog.String("user_ids", fmt.Sprintf("%v", userIds)), slog.String("relation", relation), @@ -544,10 +517,10 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { + message := "Method list_user_domains completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_user_domains"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -555,7 +528,6 @@ func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_user_domains"), slog.String("token", token), slog.String("user_id", userID), slog.String("duration", time.Since(begin).String()), diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index cf4eb7f00..51cbef5e1 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/bootstrap" ) -const message = "Method completed" - var _ bootstrap.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -32,10 +30,10 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { + message :="Method add completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "add"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -43,7 +41,6 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "add"), slog.String("thing_id", saved.ThingID), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -57,10 +54,10 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { + message :="Method view completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "view"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -68,7 +65,6 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "view"), slog.String("thing_id", saved.ThingID), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -82,10 +78,10 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { + message :="Method update completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "update"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -93,7 +89,6 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update"), slog.String("thing_id", cfg.ThingID), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -106,11 +101,11 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots // UpdateCert logs the update_cert request. It logs token, thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { - defer func(begin time.Time) { + defer func(begin time.Time) { + message:= "Method update_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_cert"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -118,7 +113,6 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_cert"), slog.String("thing_id", thingID), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -132,10 +126,10 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { + message :="Method update_connections completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "update_connections"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -143,7 +137,6 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_connections"), slog.String("thing_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -157,10 +150,10 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { + message :="Method list completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -168,7 +161,6 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list"), slog.String("token", token), slog.Uint64("offset", offset), slog.Uint64("limit", limit), @@ -183,10 +175,10 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { + message :="Method remove completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "remove"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -194,7 +186,6 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove"), slog.String("thing_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -206,10 +197,10 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { + message :="Method bootstrap completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "bootstrap"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -217,7 +208,6 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "bootstrap"), slog.String("external_id", externalID), slog.String("external_key", externalKey), slog.String("duration", time.Since(begin).String()), @@ -229,10 +219,10 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { + message :="Method change_state completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "change_state"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -240,7 +230,6 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "change_state"), slog.String("thing_id", id), slog.String("token", token), slog.String("state", state.String()), @@ -253,10 +242,10 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { + message :="Method update_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_channel_handler"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -264,7 +253,6 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_channel_handler"), slog.String("channel_id", channel.ID), slog.String("duration", time.Since(begin).String()), ) @@ -275,10 +263,10 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { + message :="Method remove_config_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "remove_config_handler"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -286,7 +274,6 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_config_handler"), slog.String("config_id", id), slog.String("duration", time.Since(begin).String()), ) @@ -297,10 +284,10 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { + message :="Method remove_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_channel_handler"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -308,7 +295,6 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_channel_handler"), slog.String("channel_id", id), slog.String("duration", time.Since(begin).String()), ) @@ -319,10 +305,10 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { + message :="Method disconnect_thing_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "disconnect_thing_handler"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -330,7 +316,6 @@ func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channel } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "disconnect_thing_handler"), slog.String("channel_id", channelID), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), diff --git a/certs/api/logging.go b/certs/api/logging.go index e10e2c6c5..2452c2421 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/certs" ) -const message = "Method completed" - var _ certs.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -32,17 +30,16 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { + message:= "Method issue_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "issue_cert"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message), - slog.String("method", "issue_cert"), slog.String("thing_id", thingID), slog.String("token", token), slog.String("ttl", ttl), @@ -56,10 +53,10 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl // ListCerts logs the list_certs request. It logs the token, thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { + message:= "Method list_certs completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_certs"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -67,7 +64,6 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_certs"), slog.String("thing_id", thingID), slog.String("token", token), slog.Uint64("offset", offset), @@ -83,10 +79,10 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { + message:= "Method list_serials completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_serials"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -94,7 +90,6 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_serials"), slog.String("token", token), slog.String("thing_id", thingID), slog.Uint64("offset", offset), @@ -110,10 +105,10 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { + message:= "Method view_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "view_cert"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -121,7 +116,6 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_cert"), slog.String("token", token), slog.String("serial_id", serialID), slog.String("duration", time.Since(begin).String()), @@ -135,10 +129,10 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { + message:= "Method revoke_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "revoke_cert"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -146,7 +140,6 @@ func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID stri } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "revoke_cert"), slog.String("token", token), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), diff --git a/coap/api/logging.go b/coap/api/logging.go index 8eb250b6b..cc56d730c 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -15,8 +15,6 @@ import ( "github.com/absmach/magistrala/pkg/messaging" ) -const logmessage = "Method completed" - var _ coap.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -37,9 +35,10 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa if msg.GetSubtopic() != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, msg.GetSubtopic()) } + message:= "Method publish completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", logmessage), + fmt.Sprintf("%s with error.", message), slog.String("method", "publish"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -47,7 +46,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa return } lm.logger.Info( - fmt.Sprintf("%s without errors.", logmessage), + fmt.Sprintf("%s without errors.", message), slog.String("method", "publish"), slog.String("channel", destChannel), slog.String("duration", time.Since(begin).String()), @@ -65,9 +64,10 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } + message:= "Method subscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", logmessage), + fmt.Sprintf("%s with error.", message), slog.String("method", "subscribe"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -75,7 +75,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi return } lm.logger.Info( - fmt.Sprintf("%s without errors.", logmessage), + fmt.Sprintf("%s without errors.", message), slog.String("method", "subscribe"), slog.String("channel", destChannel), slog.String("client", c.Token()), @@ -95,9 +95,10 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } + message:= "Method unsubscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", logmessage, err), + fmt.Sprintf("%s with error: %s.", message, err), slog.String("method", "unsubscribe"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -105,7 +106,7 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto return } lm.logger.Info( - fmt.Sprintf("%s without errors.", logmessage), + fmt.Sprintf("%s without errors.", message), slog.String("method", "unsubscribe"), slog.String("channel", destChannel), slog.String("token", token), diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 395a29cb4..5d7bfec0f 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/consumers/notifiers" ) -const message = "Method completed" - var _ notifiers.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -32,10 +30,10 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { + message:= "Method create_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_subscription"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -43,7 +41,6 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_subscription"), slog.String("id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -57,10 +54,10 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { + message:= "Method view_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "view_subscription"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -68,7 +65,6 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_subscription"), slog.String("topic", topic), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -82,10 +78,10 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { + message:= "Method list_subscriptions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_subscriptions"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -93,7 +89,6 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_subscriptions"), slog.String("topic", pm.Topic), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -107,10 +102,10 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { + message:= "Method remove_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_subscription"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -118,7 +113,6 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_subscription"), slog.String("id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -132,10 +126,10 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { + message:= "Method consume_blocking completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "consume"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -143,7 +137,6 @@ func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{ } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "consume"), slog.String("message", fmt.Sprintf("%v", msg)), slog.String("duration", time.Since(begin).String()), ) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index e83a767a1..69fadb77a 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -12,8 +12,6 @@ import ( "github.com/absmach/magistrala/pkg/groups" ) -const message = "Method completed" - var _ groups.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -30,10 +28,10 @@ func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { + message := "Method create_group %s completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -41,7 +39,6 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_group"), slog.String("group_name", g.Name), slog.String("token", token), slog.String("kind", kind), @@ -56,10 +53,10 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { + message := "Method update_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -67,7 +64,6 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_group"), slog.String("group_name", g.Name), slog.String("group_id", g.ID), slog.String("token", token), @@ -81,10 +77,10 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { + message := "Method view_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "view_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -92,7 +88,6 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_group"), slog.String("group_name", g.Name), slog.String("group_id", g.ID), slog.String("token", token), @@ -106,10 +101,10 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { + message := "Method view_group_perms completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "view_group_perms"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -117,7 +112,6 @@ func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id strin } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_group_perms"), slog.String("group_id", id), slog.String("token", token), slog.String("duration", time.Since(begin).String()), @@ -134,7 +128,6 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_groups"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -142,7 +135,6 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_groups"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), ) @@ -154,10 +146,10 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { + message := "Method enable_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "enable_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -165,7 +157,6 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "enable_group"), slog.String("group_name", g.Name), slog.String("group_id", g.ID), slog.String("token", token), @@ -179,10 +170,10 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { + message := "Method disable_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "disable_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -190,7 +181,6 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "disable_group"), slog.String("group_name", g.Name), slog.String("group_id", g.ID), slog.String("token", token), @@ -204,10 +194,10 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (mp groups.MembersPage, err error) { defer func(begin time.Time) { + message := "Method list_members completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "list_memberships"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -215,7 +205,6 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_memberships"), slog.String("group_id", groupID), slog.String("token", token), slog.String("permission", permission), @@ -228,10 +217,10 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { + message := "Method assign completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "assign"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -239,7 +228,6 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "assign"), slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), slog.String("group_id", groupID), slog.String("token", token), @@ -254,10 +242,10 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { + message := "Method unassign completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "unassign"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -265,7 +253,6 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "unassign"), slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), slog.String("group_id", groupID), slog.String("token", token), @@ -278,10 +265,10 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { + message := "Method delete_group completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "delete_group"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) diff --git a/lora/api/logging.go b/lora/api/logging.go index 437e91afe..23be010a9 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/lora" ) -const message = "Method completed" - var _ lora.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -33,10 +31,10 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { + message:= "Method create_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -44,7 +42,6 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_thing"), slog.String("thing_id", thingID), slog.String("lora_dev_eui", loraDevEUI), slog.String("duration", time.Since(begin).String()), @@ -56,10 +53,10 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { + message:= "Method update_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -67,7 +64,6 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_thing"), slog.String("thing_id", thingID), slog.String("lora_dev_eui", loraDevEUI), slog.String("duration", time.Since(begin).String()), @@ -79,10 +75,10 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { + message:= "Method remove_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -90,7 +86,6 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_thing"), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), ) @@ -101,10 +96,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { + message:= "Method create_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "create_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -112,7 +107,6 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_channel"), slog.String("channel_id", chanID), slog.String("lora_app", loraApp), slog.String("duration", time.Since(begin).String()), @@ -124,10 +118,10 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { + message:= "Method update_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "update_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -135,7 +129,6 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_channel"), slog.String("channel_id", chanID), slog.String("lora_app", loraApp), slog.String("duration", time.Since(begin).String()), @@ -147,10 +140,10 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { + message:= "Method remove_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_channel"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -158,7 +151,6 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_channel"), slog.String("channel_id", chanID), slog.String("duration", time.Since(begin).String()), ) @@ -169,10 +161,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { + message := fmt.Sprintf("Method connect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "connect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -180,7 +172,6 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "connect_thing"), slog.String("channel_id", chanID), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), @@ -196,7 +187,6 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "disconnect_thing"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -204,7 +194,6 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "disconnect_thing"), slog.String("channel_id", chanID), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), @@ -220,7 +209,6 @@ func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "publish"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -228,7 +216,6 @@ func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "publish"), slog.String("application_id", msg.ApplicationID), slog.String("device_eui", msg.DevEUI), slog.String("duration", time.Since(begin).String()), diff --git a/opcua/api/logging.go b/opcua/api/logging.go index ea0157ba1..f3eb8d3ce 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/opcua" ) -const message = "Method completed" - var _ opcua.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -33,18 +31,17 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { + message:= "Method create_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "create_thing"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_thing"), + fmt.Sprintf("%s without errors", message), slog.String("thing_id", mgxThing), slog.String("opcua_node_id", opcuaNodeID), slog.String("duration", time.Since(begin).String()), @@ -56,18 +53,17 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { + message:= "Method update_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_thing"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_thing"), + fmt.Sprintf("%s without errors", message), slog.String("thing_id", mgxThing), slog.String("opcua_node_id", opcuaNodeID), slog.String("duration", time.Since(begin).String()), @@ -79,18 +75,17 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { + message:= "Method remove_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_thing"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_thing"), + fmt.Sprintf("%s without errors", message), slog.String("thing_id", mgxThing), slog.String("duration", time.Since(begin).String()), ) @@ -101,18 +96,17 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { + message:= "Method create_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "create_channel"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "create_channel"), + fmt.Sprintf("%s without errors", message), slog.String("channel_id", mgxChan), slog.String("opcua_server_uri", opcuaServerURI), slog.String("duration", time.Since(begin).String()), @@ -124,18 +118,17 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { + message:= "Method update_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_channel"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_channel"), + fmt.Sprintf("%s without errors", message), slog.String("channel_id", mgxChanID), slog.String("opcua_server_uri", opcuaServerURI), slog.String("duration", time.Since(begin).String()), @@ -147,18 +140,17 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { + message:= "Method remove_channel completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_channel"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_channel"), + fmt.Sprintf("%s without errors", message), slog.String("channel_id", mgxChanID), slog.String("duration", time.Since(begin).String()), ) @@ -169,18 +161,17 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { + message:= "Method connect_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "connect_thing"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "connect_thing"), + fmt.Sprintf("%s without errors", message), slog.String("channel_id", mgxChanID), slog.String("thing_id", mgxThingID), slog.String("duration", time.Since(begin).String()), @@ -192,19 +183,17 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disconnect_thing mgx-%s : mgx-%s, took %s to complete", mgxChanID, mgxThingID, time.Since(begin)) + message := "Method disconnect_thing completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "disconnect_thing"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "disconnect_thing"), + fmt.Sprintf("%s without errors", message), slog.String("channel_id", mgxChanID), slog.String("thing_id", mgxThingID), slog.String("duration", time.Since(begin).String()), @@ -216,19 +205,17 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, identifier string) (nodes []opcua.BrowsedNode, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method browse server URI %s and node %s;%s, took %s to complete", serverURI, namespace, identifier, time.Since(begin)) + message := "Method browse completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "browse"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "browse"), + fmt.Sprintf("%s without errors", message), slog.String("server_uri", serverURI), slog.String("namespace", namespace), slog.String("identifier", identifier), diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 53d068ed6..84b8ce8f6 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -26,18 +26,17 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { defer func(begin time.Time) { + message:= "Method auth_connect completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "auth_connect"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "auth_connect"), + fmt.Sprintf("%s without errors", message), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -48,18 +47,17 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { + message:= "Method auth_publish completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "auth_publish"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "auth_publish"), + fmt.Sprintf("%s without errors", message), slog.String("topic", *topic), slog.String("duration", time.Since(begin).String()), ) @@ -71,10 +69,10 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { + message:= "Method auth_subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "auth_subscribe"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -82,7 +80,6 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "auth_subscribe"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -93,18 +90,17 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { defer func(begin time.Time) { + message:= "Method connect completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "connect"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "connect"), + fmt.Sprintf("%s without errors", message), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -115,10 +111,10 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { defer func(begin time.Time) { + message:= "Method disconnect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "disconnect"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -126,7 +122,6 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { } lm.logger.Info( fmt.Sprintf("%s without errors.", message), - slog.String("method", "disconnect"), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -138,18 +133,17 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { + message:= "Method publish completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "publish"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "publish"), + fmt.Sprintf("%s without errors", message), slog.String("channel_topic", *topic), slog.String("duration", time.Since(begin).String()), ) @@ -161,18 +155,17 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { + message:= "Method subscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "subscribe"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "subscribe"), + fmt.Sprintf("%s without errors", message), slog.String("duration", time.Since(begin).String()), ) }(time.Now()) @@ -183,19 +176,17 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (e // Unsubscribe implements session.Handler. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method unsubscribe took %s to complete", time.Since(begin)) + message := "Method unsubscribe completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "unsubscribe"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "unsubscribe"), + fmt.Sprintf("%s without errors", message), slog.String("topics", fmt.Sprintf("%v", topics)), slog.String("duration", time.Since(begin).String()), ) diff --git a/provision/api/logging.go b/provision/api/logging.go index 792c58ae7..d6bf0363d 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -31,7 +31,6 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), - slog.String("method", "provision"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -39,7 +38,6 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri } lm.logger.Info( fmt.Sprintf("%s without errors", message), - slog.String("method", "provision"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), ) @@ -50,11 +48,10 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key string, err error) { defer func(begin time.Time) { - message := "Method completed" + message := "Method cert completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s", message, err), - slog.String("method", "cert"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -62,7 +59,6 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st } lm.logger.Info( fmt.Sprintf("%s without errors", message), - slog.String("method", "cert"), slog.String("token", token), slog.String("thing_id", thingID), slog.String("duration", time.Since(begin).String()), @@ -74,11 +70,10 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, err error) { defer func(begin time.Time) { - message := "Method completed" + message := "Method mapping completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error: %s", message, err), - slog.String("method", "mapping"), + fmt.Sprintf("%s with error", message), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), ) @@ -86,7 +81,6 @@ func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, } lm.logger.Info( fmt.Sprintf("%s without errors", message), - slog.String("method", "mapping"), slog.String("token", token), slog.String("duration", time.Since(begin).String()), ) diff --git a/readers/api/logging.go b/readers/api/logging.go index 4afe8d966..83b68af7d 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -33,7 +33,7 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p message := "Method completed" if err != nil { lm.logger.Warn( - fmt.Sprintf("%s with error.", message), + fmt.Sprintf("%s with error", message), slog.String("method", "read_all"), slog.String("error", err.Error()), slog.String("duration", time.Since(begin).String()), @@ -41,7 +41,7 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p return } lm.logger.Info( - fmt.Sprintf("%s without errors.", message), + fmt.Sprintf("%s without errors", message), slog.String("method", "read_all"), slog.String("channel_ID", chanID), slog.String("query", fmt.Sprintf("%v", rpm)), diff --git a/users/api/logging.go b/users/api/logging.go index 1fc8ce345..871856a36 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/magistrala/users" ) -const message = "Method completed" - var _ users.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -32,22 +30,16 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "register_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Register client failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "register_client"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Register client completed successfully", args...) }(time.Now()) return lm.svc.RegisterClient(ctx, token, client) } @@ -56,26 +48,19 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "issue_token"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) - return - } - message := fmt.Sprintf("%s without errors.", message) - args := []interface{}{ - "method", "issue_token", - "identity", identity, - "domain_id", domainID, - "duration", time.Since(begin).String(), + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("identity", identity), slog.String("domain_ID", domainID)), } if t.AccessType != "" { args = append(args, "access_type", t.AccessType) } - lm.logger.Info(message, args...) + if err != nil { + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Issue token failed to complete successfully", args...) + return + } + lm.logger.Info("Issue token completed successfully", args...) }(time.Now()) return lm.svc.IssueToken(ctx, identity, secret, domainID) } @@ -84,25 +69,19 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d // If the request fails, it logs the error. func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "refresh_token"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) - return - } - message := fmt.Sprintf("%s without errors.", message) - args := []interface{}{ - "method", "refresh_token", - "domain_id", domainID, - "duration", time.Since(begin).String(), + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.String("domain_ID", domainID), } if t.AccessType != "" { args = append(args, "access_type", t.AccessType) } - lm.logger.Info(message, args...) + if err != nil { + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Refresh token failed to complete successfully", args...) + return + } + lm.logger.Info("Refresh token completed successfully", args...) }(time.Now()) return lm.svc.RefreshToken(ctx, refreshToken, domainID) } @@ -111,22 +90,16 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", id)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "view_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View client failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_client"), - slog.String("client_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View client completed successfully", args...) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } @@ -135,21 +108,16 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "view_profile"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View profile failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_profile"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View profile completed successfully", args...) }(time.Now()) return lm.svc.ViewProfile(ctx, token) } @@ -158,21 +126,16 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Any("total", pm.Total), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "list_clients"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List clients failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_clients"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List clients completed successfully", args...) }(time.Now()) return lm.svc.ListClients(ctx, token, pm) } @@ -181,22 +144,16 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "update_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_client"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client completed successfully", args...) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } @@ -205,22 +162,16 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_client_tags"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client tags failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_client_tags"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client tags completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } @@ -229,23 +180,16 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("identity", identity)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_client_identity"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client identity failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_client_identity"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("identity", identity), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client identity completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientIdentity(ctx, token, id, identity) } @@ -254,22 +198,16 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_client_secret"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client secret failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_client_secret"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client secret completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } @@ -278,22 +216,15 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "generate_reset_token"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Generate reset token failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "generate_reset_token"), - slog.String("email", email), - slog.String("host", host), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Generate reset token completed successfully", args...) }(time.Now()) return lm.svc.GenerateResetToken(ctx, email, host) } @@ -302,21 +233,15 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "reset_secret"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Reset secret failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "reset_secret"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Reset secret completed successfully", args...) }(time.Now()) return lm.svc.ResetSecret(ctx, token, secret) } @@ -325,24 +250,15 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "send_password_reset"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Send password reset failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "send_password_reset"), - slog.String("host", host), - slog.String("email", email), - slog.String("user", user), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Send password reset completed successfully", args...) }(time.Now()) return lm.svc.SendPasswordReset(ctx, host, email, user, token) } @@ -351,23 +267,16 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("role", client.Role.String())), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_client_role"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client role failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_client_role"), - slog.String("client_id", c.ID), - slog.String("role", client.Role.String()), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client role completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientRole(ctx, token, client) } @@ -376,22 +285,16 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "enable_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Enable client failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "enable_client"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Enable client completed successfully", args...) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } @@ -400,22 +303,16 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "disable_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disable client failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "disable_client"), - slog.String("client_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disable client completed successfully", args...) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } @@ -424,24 +321,16 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_members %d members completed", mp.Total) + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + slog.Any("total", mp.Total), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "list_members"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List members failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_members"), - slog.String("object_kind", objectKind), - slog.String("object_id", objectID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List members completed successfully", args...) }(time.Now()) return lm.svc.ListMembers(ctx, token, objectKind, objectID, cp) } @@ -449,22 +338,15 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, // Identify logs the identify request. It logs the token and the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { + args:= []interface{}{ + slog.Any("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "identify"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Identify failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "identify"), - slog.String("client_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Identify completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, token) } From 30b7b47855ee266c0996bec8f6b5d61e077cff24 Mon Sep 17 00:00:00 2001 From: Musilah Date: Wed, 17 Jan 2024 18:28:54 +0300 Subject: [PATCH 04/23] fix users svc Signed-off-by: Musilah --- bootstrap/api/logging.go | 28 +++++++------- certs/api/logging.go | 10 ++--- coap/api/logging.go | 6 +-- consumers/notifiers/api/logging.go | 10 ++--- lora/api/logging.go | 12 +++--- opcua/api/logging.go | 14 +++---- pkg/messaging/handler/logging.go | 16 ++++---- users/api/logging.go | 62 +++++++++++++++--------------- 8 files changed, 78 insertions(+), 80 deletions(-) diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 51cbef5e1..b92180ec1 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message :="Method add completed" + message := "Method add completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -54,7 +54,7 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message :="Method view completed" + message := "Method view completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -78,7 +78,7 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { - message :="Method update completed" + message := "Method update completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -101,8 +101,8 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots // UpdateCert logs the update_cert request. It logs token, thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { - defer func(begin time.Time) { - message:= "Method update_cert completed" + defer func(begin time.Time) { + message := "Method update_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -126,7 +126,7 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { - message :="Method update_connections completed" + message := "Method update_connections completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -150,7 +150,7 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { - message :="Method list completed" + message := "Method list completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -175,7 +175,7 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message :="Method remove completed" + message := "Method remove completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -197,7 +197,7 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message :="Method bootstrap completed" + message := "Method bootstrap completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -219,7 +219,7 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { - message :="Method change_state completed" + message := "Method change_state completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -242,7 +242,7 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { - message :="Method update_channel_handler completed" + message := "Method update_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -263,7 +263,7 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message :="Method remove_config_handler completed" + message := "Method remove_config_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -284,7 +284,7 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message :="Method remove_channel_handler completed" + message := "Method remove_channel_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -305,7 +305,7 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { - message :="Method disconnect_thing_handler completed" + message := "Method disconnect_thing_handler completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/certs/api/logging.go b/certs/api/logging.go index 2452c2421..57a3a79c0 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { - message:= "Method issue_cert completed" + message := "Method issue_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -53,7 +53,7 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl // ListCerts logs the list_certs request. It logs the token, thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message:= "Method list_certs completed" + message := "Method list_certs completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -79,7 +79,7 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message:= "Method list_serials completed" + message := "Method list_serials completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -105,7 +105,7 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { - message:= "Method view_cert completed" + message := "Method view_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -129,7 +129,7 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { - message:= "Method revoke_cert completed" + message := "Method revoke_cert completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/coap/api/logging.go b/coap/api/logging.go index cc56d730c..7432c3a19 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -35,7 +35,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa if msg.GetSubtopic() != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, msg.GetSubtopic()) } - message:= "Method publish completed" + message := "Method publish completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -64,7 +64,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message:= "Method subscribe completed" + message := "Method subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -95,7 +95,7 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto if subtopic != "" { destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) } - message:= "Method unsubscribe completed" + message := "Method unsubscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 5d7bfec0f..27f8e970d 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { - message:= "Method create_subscription completed" + message := "Method create_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -54,7 +54,7 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { - message:= "Method view_subscription completed" + message := "Method view_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -78,7 +78,7 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { - message:= "Method list_subscriptions completed" + message := "Method list_subscriptions completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -102,7 +102,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message:= "Method remove_subscription completed" + message := "Method remove_subscription completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -126,7 +126,7 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { - message:= "Method consume_blocking completed" + message := "Method consume_blocking completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/lora/api/logging.go b/lora/api/logging.go index 23be010a9..0b92fdc0c 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -31,7 +31,7 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message:= "Method create_thing completed" + message := "Method create_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -53,7 +53,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message:= "Method update_thing completed" + message := "Method update_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -75,7 +75,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { - message:= "Method remove_thing completed" + message := "Method remove_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -96,7 +96,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message:= "Method create_channel completed" + message := "Method create_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -118,7 +118,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message:= "Method update_channel completed" + message := "Method update_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), @@ -140,7 +140,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { - message:= "Method remove_channel completed" + message := "Method remove_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error.", message), diff --git a/opcua/api/logging.go b/opcua/api/logging.go index f3eb8d3ce..ac41ce6dc 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -31,7 +31,7 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message:= "Method create_thing completed" + message := "Method create_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -53,7 +53,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message:= "Method update_thing completed" + message := "Method update_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -75,7 +75,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { - message:= "Method remove_thing completed" + message := "Method remove_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -96,7 +96,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message:= "Method create_channel completed" + message := "Method create_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -118,7 +118,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message:= "Method update_channel completed" + message := "Method update_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -140,7 +140,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { - message:= "Method remove_channel completed" + message := "Method remove_channel completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -161,7 +161,7 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message:= "Method connect_thing completed" + message := "Method connect_thing completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 84b8ce8f6..470cb2a79 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -14,8 +14,6 @@ import ( "github.com/absmach/mproxy/pkg/session" ) -const message = "Method completed" - var _ session.Handler = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -26,7 +24,7 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message:= "Method auth_connect completed" + message := "Method auth_connect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -47,7 +45,7 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message:= "Method auth_publish completed" + message := "Method auth_publish completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -69,7 +67,7 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message:= "Method auth_subscribe completed" + message := "Method auth_subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -90,7 +88,7 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { defer func(begin time.Time) { - message:= "Method connect completed" + message := "Method connect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -111,7 +109,7 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message:= "Method disconnect completed" + message := "Method disconnect completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error: %s.", message, err), @@ -133,7 +131,7 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message:= "Method publish completed" + message := "Method publish completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), @@ -155,7 +153,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message:= "Method subscribe completed" + message := "Method subscribe completed" if err != nil { lm.logger.Warn( fmt.Sprintf("%s with error", message), diff --git a/users/api/logging.go b/users/api/logging.go index 871856a36..a066b5b59 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -30,9 +30,9 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -48,9 +48,9 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("identity", identity), slog.String("domain_ID", domainID)), + slog.Group("user", slog.String("identity", identity), slog.String("domain_id", domainID)), } if t.AccessType != "" { args = append(args, "access_type", t.AccessType) @@ -69,9 +69,9 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d // If the request fails, it logs the error. func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.String("domain_ID", domainID), + slog.String("domain_id", domainID), } if t.AccessType != "" { args = append(args, "access_type", t.AccessType) @@ -90,9 +90,9 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", id)), + slog.Group("user", slog.String("name", c.Name), slog.String("id", id)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -108,9 +108,9 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -126,7 +126,7 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), slog.Any("total", pm.Total), } @@ -144,9 +144,9 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_name", c.Name), slog.String("client_ID", c.ID)), + slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -162,9 +162,9 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), + slog.Group("user", slog.String("id", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -176,13 +176,13 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, return lm.svc.UpdateClientTags(ctx, token, client) } -// UpdateClientIdentity logs the update_client_identity request. It logs the client id and token and the time it took to complete the request. +// UpdateClientIdentity logs the update_identity request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("identity", identity)), + slog.Group("user", slog.String("id", c.ID), slog.String("identity", identity)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -198,9 +198,9 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -216,7 +216,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), } if err != nil { @@ -233,7 +233,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), } if err != nil { @@ -250,7 +250,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), } if err != nil { @@ -267,9 +267,9 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("role", client.Role.String())), + slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -285,9 +285,9 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -303,9 +303,9 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), - slog.Group("user", slog.String("client_ID", c.ID), slog.String("name", c.Name)), + slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -321,7 +321,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), slog.Any("total", mp.Total), } @@ -338,7 +338,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, // Identify logs the identify request. It logs the token and the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { - args:= []interface{}{ + args := []interface{}{ slog.Any("duration", time.Since(begin).String()), } if err != nil { From 9edb27369b9833b1ef187f14d7d68a1826758c03 Mon Sep 17 00:00:00 2001 From: Musilah Date: Thu, 18 Jan 2024 12:36:27 +0300 Subject: [PATCH 05/23] fix twins and ws Signed-off-by: Musilah --- twins/api/logging.go | 149 +++++++++++++++---------------------------- users/api/logging.go | 72 ++++++++++----------- ws/api/logging.go | 25 +++----- 3 files changed, 98 insertions(+), 148 deletions(-) diff --git a/twins/api/logging.go b/twins/api/logging.go index 711320835..35c4285fa 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -15,8 +14,6 @@ import ( "github.com/absmach/magistrala/twins" ) -const message = "Method completed" - var _ twins.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -31,23 +28,16 @@ func LoggingMiddleware(svc twins.Service, logger *slog.Logger) twins.Service { func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (tw twins.Twin, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.Group("twin", slog.String("id", twin.ID), slog.String("name", twin.Name), slog.Any("definition", def)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "add_twin"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Add twin failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "add_twin"), - slog.String("id", twin.ID), - slog.String("token", token), - slog.String("definition", fmt.Sprintf("%v", def)), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Add twin completed successfully.", args...) }(time.Now()) return lm.svc.AddTwin(ctx, token, twin, def) @@ -55,23 +45,16 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.Group("twin", slog.String("id", twin.ID), slog.String("name", twin.Name), slog.Any("definition", def)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_twin"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update twin failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_twin"), - slog.String("id", twin.ID), - slog.String("token", token), - slog.String("definition", fmt.Sprintf("%v", def)), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update twin completed successfully.", args...) }(time.Now()) return lm.svc.UpdateTwin(ctx, token, twin, def) @@ -79,22 +62,16 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) (tw twins.Twin, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("twin_id", twinID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "view_twin"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View twin failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_twin"), - slog.String("id", twinID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View twin completed successfully.", args...) }(time.Now()) return lm.svc.ViewTwin(ctx, token, twinID) @@ -102,23 +79,18 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset, limit uint64, name string, metadata twins.Metadata) (page twins.Page, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("name", name), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "list_twins"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List twins failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_twins"), - slog.String("token", token), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List twins completed successfully.", args...) }(time.Now()) return lm.svc.ListTwins(ctx, token, offset, limit, name, metadata) @@ -126,20 +98,16 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Message) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("payload", string(msg.Payload)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "save_states"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Save states failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "save_states"), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Save states completed successfully.", args...) }(time.Now()) return lm.svc.SaveStates(ctx, msg) @@ -147,23 +115,18 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offset, limit uint64, twinID string) (page twins.StatesPage, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("twin_id", twinID), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "list_states"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List states failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_states"), - slog.String("token", token), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List states completed successfully.", args...) }(time.Now()) return lm.svc.ListStates(ctx, token, offset, limit, twinID) @@ -171,22 +134,16 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID string) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("twin_id", twinID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "remove_twin"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove twin failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "remove_twin"), - slog.String("token", token), - slog.String("twin_ID", twinID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove twin completed successfully.", args...) }(time.Now()) return lm.svc.RemoveTwin(ctx, token, twinID) diff --git a/users/api/logging.go b/users/api/logging.go index a066b5b59..a1c8c9c46 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -31,12 +31,12 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Register client failed to complete successfully", args...) + lm.logger.Error("Register client failed to complete successfully", args...) return } lm.logger.Info("Register client completed successfully", args...) @@ -49,7 +49,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("identity", identity), slog.String("domain_id", domainID)), } if t.AccessType != "" { @@ -57,7 +57,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Issue token failed to complete successfully", args...) + lm.logger.Error("Issue token failed to complete successfully", args...) return } lm.logger.Info("Issue token completed successfully", args...) @@ -70,7 +70,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.String("domain_id", domainID), } if t.AccessType != "" { @@ -78,7 +78,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Refresh token failed to complete successfully", args...) + lm.logger.Error("Refresh token failed to complete successfully", args...) return } lm.logger.Info("Refresh token completed successfully", args...) @@ -91,12 +91,12 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("name", c.Name), slog.String("id", id)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("View client failed to complete successfully", args...) + lm.logger.Error("View client failed to complete successfully", args...) return } lm.logger.Info("View client completed successfully", args...) @@ -109,12 +109,12 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("View profile failed to complete successfully", args...) + lm.logger.Error("View profile failed to complete successfully", args...) return } lm.logger.Info("View profile completed successfully", args...) @@ -127,12 +127,12 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Any("total", pm.Total), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List clients failed to complete successfully", args...) + lm.logger.Error("List clients failed to complete successfully", args...) return } lm.logger.Info("List clients completed successfully", args...) @@ -145,12 +145,12 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client failed to complete successfully", args...) + lm.logger.Error("Update client failed to complete successfully", args...) return } lm.logger.Info("Update client completed successfully", args...) @@ -163,12 +163,12 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client tags failed to complete successfully", args...) + lm.logger.Error("Update client tags failed to complete successfully", args...) return } lm.logger.Info("Update client tags completed successfully", args...) @@ -181,12 +181,12 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("identity", identity)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client identity failed to complete successfully", args...) + lm.logger.Error("Update client identity failed to complete successfully", args...) return } lm.logger.Info("Update client identity completed successfully", args...) @@ -199,12 +199,12 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client secret failed to complete successfully", args...) + lm.logger.Error("Update client secret failed to complete successfully", args...) return } lm.logger.Info("Update client secret completed successfully", args...) @@ -217,11 +217,11 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Generate reset token failed to complete successfully", args...) + lm.logger.Error("Generate reset token failed to complete successfully", args...) return } lm.logger.Info("Generate reset token completed successfully", args...) @@ -234,11 +234,11 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Reset secret failed to complete successfully", args...) + lm.logger.Error("Reset secret failed to complete successfully", args...) return } lm.logger.Info("Reset secret completed successfully", args...) @@ -251,11 +251,11 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Send password reset failed to complete successfully", args...) + lm.logger.Error("Send password reset failed to complete successfully", args...) return } lm.logger.Info("Send password reset completed successfully", args...) @@ -268,12 +268,12 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client role failed to complete successfully", args...) + lm.logger.Error("Update client role failed to complete successfully", args...) return } lm.logger.Info("Update client role completed successfully", args...) @@ -286,12 +286,12 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Enable client failed to complete successfully", args...) + lm.logger.Error("Enable client failed to complete successfully", args...) return } lm.logger.Info("Enable client completed successfully", args...) @@ -304,12 +304,12 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Disable client failed to complete successfully", args...) + lm.logger.Error("Disable client failed to complete successfully", args...) return } lm.logger.Info("Disable client completed successfully", args...) @@ -322,12 +322,12 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), slog.Any("total", mp.Total), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List members failed to complete successfully", args...) + lm.logger.Error("List members failed to complete successfully", args...) return } lm.logger.Info("List members completed successfully", args...) @@ -339,11 +339,11 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin).String()), + slog.Any("duration", time.Since(begin)), } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Identify failed to complete successfully", args...) + lm.logger.Error("Identify failed to complete successfully", args...) return } lm.logger.Info("Identify completed successfully", args...) diff --git a/ws/api/logging.go b/ws/api/logging.go index 8084b725b..0017682ba 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -5,7 +5,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -28,27 +27,21 @@ func LoggingMiddleware(svc ws.Service, logger *slog.Logger) ws.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.Any("duration", time.Since(begin)), + slog.String("thing_key", thingKey), + slog.String("channel_id", chanID), + } destChannel := chanID if subtopic != "" { - destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) + args = append(args, "subtopic", subtopic, "channel", destChannel) } - message := "Method completed" if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "subscribe"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Subscibe failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "subscribe"), - slog.String("channel", destChannel), - slog.String("thing_key", thingKey), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Subscribe completed successfully.", args...) }(time.Now()) return lm.svc.Subscribe(ctx, thingKey, chanID, subtopic, c) From 44b9f7834a21e7f8907a3d6deb016c10a1a7fb18 Mon Sep 17 00:00:00 2001 From: Musilah Date: Fri, 19 Jan 2024 16:39:07 +0300 Subject: [PATCH 06/23] Update more services Signed-off-by: Musilah --- opcua/api/logging.go | 23 +-- pkg/messaging/handler/logging.go | 139 +++++-------- provision/api/logging.go | 58 +++--- readers/api/logging.go | 29 ++- things/api/logging.go | 336 +++++++++++++------------------ twins/api/logging.go | 51 +++-- users/api/logging.go | 46 ++--- ws/api/logging.go | 2 +- 8 files changed, 298 insertions(+), 386 deletions(-) diff --git a/opcua/api/logging.go b/opcua/api/logging.go index ac41ce6dc..d66190c40 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -31,21 +31,20 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := "Method create_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "thing", + slog.String("thing_id", mgxThing), + slog.String("opcua_node_id", opcuaNodeID), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create thing failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("thing_id", mgxThing), - slog.String("opcua_node_id", opcuaNodeID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create thing completed successfully.", args...) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 470cb2a79..4b2e4feb3 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -7,7 +7,6 @@ package handler import ( "context" - "fmt" "log/slog" "time" @@ -24,19 +23,15 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method auth_connect completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("AuthConnect failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("AuthConnect completed successfully", args...) }(time.Now()) return lm.svc.AuthConnect(ctx) @@ -45,20 +40,17 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := "Method auth_publish completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("topic", *topic), + slog.Any("payload", payload), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("AuthPublish failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("topic", *topic), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("AuthPublish completed successfully", args...) }(time.Now()) return lm.svc.AuthPublish(ctx, topic, payload) @@ -67,19 +59,16 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := "Method auth_subscribe completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("topics", topics), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("AuthSubscribe failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("AuthSubscribe completed successfully", args...) }(time.Now()) return lm.svc.AuthSubscribe(ctx, topics) @@ -88,19 +77,15 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method connect completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Connect failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Connect completed successfully", args...) }(time.Now()) return lm.svc.Connect(ctx) @@ -109,19 +94,15 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { defer func(begin time.Time) { - message := "Method disconnect completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disconnect failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disconnect completed successfully", args...) }(time.Now()) return lm.svc.Disconnect(ctx) @@ -131,20 +112,17 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload *[]byte) (err error) { defer func(begin time.Time) { - message := "Method publish completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("topic", topic), + slog.Any("payload", payload), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Publish failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_topic", *topic), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Publish completed successfully", args...) }(time.Now()) return lm.svc.Publish(ctx, topic, payload) @@ -153,19 +131,16 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := "Method subscribe completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("topics", topics), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Subscribe failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Subscribe completed successfully", args...) }(time.Now()) return lm.svc.Subscribe(ctx, topics) @@ -174,20 +149,16 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (e // Unsubscribe implements session.Handler. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) (err error) { defer func(begin time.Time) { - message := "Method unsubscribe completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("topics", topics), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Unsubscribe failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("topics", fmt.Sprintf("%v", topics)), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Unsubscribe completed successfully", args...) }(time.Now()) return lm.svc.Unsubscribe(ctx, topics) diff --git a/provision/api/logging.go b/provision/api/logging.go index d6bf0363d..1c9c0a645 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -6,7 +6,6 @@ package api import ( - "fmt" "log/slog" "time" @@ -27,20 +26,18 @@ func NewLoggingMiddleware(svc provision.Service, logger *slog.Logger) provision. func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey string) (res provision.Result, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method provision for things: %v took completed", res.Things) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("name", name), + slog.String("external_id", externalID), + slog.String("external_key", externalKey), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Provision failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Provision completed successfully.", args...) }(time.Now()) return lm.svc.Provision(token, name, externalID, externalKey) @@ -48,21 +45,17 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key string, err error) { defer func(begin time.Time) { - message := "Method cert completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + slog.String("duration", duration), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Cert failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("token", token), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Cert completed successfully.", args...) }(time.Now()) return lm.svc.Cert(token, thingID, duration) @@ -70,20 +63,15 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, err error) { defer func(begin time.Time) { - message := "Method mapping completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Mapping failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Mapping completed successfully.", args...) }(time.Now()) return lm.svc.Mapping(token) diff --git a/readers/api/logging.go b/readers/api/logging.go index 83b68af7d..a04d5145d 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -6,7 +6,6 @@ package api import ( - "fmt" "log/slog" "time" @@ -30,23 +29,23 @@ func LoggingMiddleware(svc readers.MessageRepository, logger *slog.Logger) reade func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (page readers.MessagesPage, err error) { defer func(begin time.Time) { - message := "Method completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_ID", chanID), + slog.Group( + "page_metadata", + slog.Any("offset", rpm.Offset), + slog.Any("limit", rpm.Limit), + slog.String("subtopic", rpm.Subtopic), + slog.String("publisher", rpm.Publisher), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "read_all"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Read all failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("method", "read_all"), - slog.String("channel_ID", chanID), - slog.String("query", fmt.Sprintf("%v", rpm)), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Read all completed successfully.", args...) }(time.Now()) return lm.svc.ReadAll(chanID, rpm) diff --git a/things/api/logging.go b/things/api/logging.go index 91dde3743..777992be6 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -5,7 +5,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -14,8 +13,6 @@ import ( "github.com/absmach/magistrala/things" ) -const message = "Method completed" - var _ things.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { @@ -29,333 +26,270 @@ func LoggingMiddleware(svc things.Service, logger *slog.Logger) things.Service { func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, clients ...mgclients.Client) (cs []mgclients.Client, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method create_things %d things completed", len(cs)) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), slog.Any("no_of_things", len(clients)), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("mathod", "create_things"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create thing failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("mathod", "create_things"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create thing completed successfully.", args...) }(time.Now()) return lm.svc.CreateThings(ctx, token, clients...) } func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "view_thing"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View client failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_thing"), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View client completed successfully.", args...) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "view_thing_permissions"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View client permissions failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "view_thing_permissions"), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View client permissions completed successfully.", args...) }(time.Now()) return lm.svc.ViewClientPerms(ctx, token, id) } func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("user_id", reqUserID), + slog.Group( + "page", + slog.Any("limit", pm.Limit), + slog.Any("offset", pm.Offset), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "list_things"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List clients failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_things"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List clients completed successfully.", args...) }(time.Now()) return lm.svc.ListClients(ctx, token, reqUserID, pm) } func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "client", + slog.String("id", client.ID), + slog.String("name", client.Name), + slog.Any("metadata", client.Metadata), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_thing_name_and_metadata"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_thing_name_and_metadata"), - slog.String("thing_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client completed successfully.", args...) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "client", + slog.String("id", client.ID), + slog.Any("tags", client.Tags), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "update_thing_tags"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args := append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client tags failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_thing_tags"), - slog.String("thing_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client tags completed successfully.", args...) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "things", + slog.String("id", c.ID), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("method", "update_thing_secret"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update client secret failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "update_thing_secret"), - slog.String("thing_id", c.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update client secret completed successfully.", args...) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with errors.", message), - slog.String("method", "enable_thing"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Enable client failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "enable_thing"), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Enable client completed successfully.", args...) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "disable_thing"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disable client failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "disable_thing"), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disable client completed successfully.", args...) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, channelID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", channelID), + slog.Group( + "page", + slog.Any("offset", cp.Offset), + slog.Any("limit", cp.Limit), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "list_things_by_channel"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List clients by group failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "list_things_by_channel"), - slog.String("channel_id", channelID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List clients by group completed successfully.", args...) }(time.Now()) return lm.svc.ListClientsByGroup(ctx, token, channelID, cp) } func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id string, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "thing", + slog.String("key", key), + slog.String("id", id), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "identify"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Identify failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "identify"), - slog.String("thing_id", id), - slog.String("thing_key", key), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Identify completed successfully.", args...) }(time.Now()) return lm.svc.Identify(ctx, key) } func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (id string, err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_key", req.Subject), + slog.String("channel_id", req.Object), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "authorise"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Authorize failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "authorize"), - slog.String("thing_key", req.Subject), - slog.String("channel_id", req.Object), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Authorize completed successfully.", args...) }(time.Now()) return lm.svc.Authorize(ctx, req) } func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", id), + slog.Any("user_ids", userids), + slog.String("relation", relation), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "share"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Share failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "share"), - slog.String("thing_id", id), - slog.String("user_IDs", fmt.Sprintf("%v", userids)), - slog.String("relation", relation), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Share completed successfully.", args...) }(time.Now()) return lm.svc.Share(ctx, token, id, relation, userids...) } func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", id), + slog.Any("user_ids", userids), + slog.String("relation", relation), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "unshare"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Unshare failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "unshare"), - slog.String("thing_id", id), - slog.String("user_IDs", fmt.Sprintf("%v", userids)), - slog.String("relation", relation), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Unshare completed successfully.", args...) }(time.Now()) return lm.svc.Unshare(ctx, token, id, relation, userids...) } func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "delete_client"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Delete client failed to complete successfully.", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "delete_client"), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Delete client completed successfully.", args...) }(time.Now()) return lm.svc.DeleteClient(ctx, token, id) } diff --git a/twins/api/logging.go b/twins/api/logging.go index 35c4285fa..e8c2cac50 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -29,8 +29,13 @@ func LoggingMiddleware(svc twins.Service, logger *slog.Logger) twins.Service { func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (tw twins.Twin, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("twin", slog.String("id", twin.ID), slog.String("name", twin.Name), slog.Any("definition", def)), + slog.String("duration", time.Since(begin).String()), + slog.Group( + "twin", + slog.String("id", twin.ID), + slog.String("name", twin.Name), + slog.Group("definition", slog.Any("def_id", def.ID)), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -46,8 +51,13 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("twin", slog.String("id", twin.ID), slog.String("name", twin.Name), slog.Any("definition", def)), + slog.String("duration", time.Since(begin).String()), + slog.Group( + "twin", + slog.String("id", twin.ID), + slog.String("name", twin.Name), + slog.Group("definition", slog.Any("def_id", def.ID)), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -63,7 +73,7 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) (tw twins.Twin, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), } if err != nil { @@ -80,10 +90,13 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset, limit uint64, name string, metadata twins.Metadata) (page twins.Page, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.String("name", name), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), + slog.String("duration", time.Since(begin).String()), + slog.Group( + "page", + slog.String("name", name), + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -99,8 +112,13 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Message) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.String("payload", string(msg.Payload)), + slog.String("duration", time.Since(begin).String()), + slog.Group( + "payload", + slog.String("subtopic", msg.Subtopic), + slog.String("channel", msg.Channel), + slog.String("publisher", msg.Publisher), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -116,10 +134,13 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offset, limit uint64, twinID string) (page twins.StatesPage, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), + slog.Group( + "page", + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -135,7 +156,7 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), } if err != nil { diff --git a/users/api/logging.go b/users/api/logging.go index a1c8c9c46..f0bc9e8f2 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -31,7 +31,7 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { @@ -49,8 +49,8 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("user", slog.String("identity", identity), slog.String("domain_id", domainID)), + slog.String("duration", time.Since(begin).String()), + slog.String("domain_id", domainID), } if t.AccessType != "" { args = append(args, "access_type", t.AccessType) @@ -70,7 +70,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.String("domain_id", domainID), } if t.AccessType != "" { @@ -91,7 +91,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("name", c.Name), slog.String("id", id)), } if err != nil { @@ -109,7 +109,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { @@ -127,8 +127,8 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Any("total", pm.Total), + slog.String("duration", time.Since(begin).String()), + slog.Any("number_of_clients", pm.Total), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -145,8 +145,8 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), + slog.String("duration", time.Since(begin).String()), + slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), slog.Any("metadata", c.Metadata), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -163,7 +163,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), } if err != nil { @@ -181,7 +181,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("identity", identity)), } if err != nil { @@ -199,7 +199,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { @@ -217,7 +217,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -234,7 +234,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -251,7 +251,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -268,7 +268,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), } if err != nil { @@ -286,8 +286,8 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.String("duration", time.Since(begin).String()), + slog.String("id", c.ID), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -304,8 +304,8 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), - slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.String("duration", time.Since(begin).String()), + slog.String("id", c.ID), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -322,7 +322,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.Any("total", mp.Total), } if err != nil { @@ -339,7 +339,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), } if err != nil { args = append(args, slog.String("error", err.Error())) diff --git a/ws/api/logging.go b/ws/api/logging.go index 0017682ba..44b56e4f4 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -28,7 +28,7 @@ func LoggingMiddleware(svc ws.Service, logger *slog.Logger) ws.Service { func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) (err error) { defer func(begin time.Time) { args := []interface{}{ - slog.Any("duration", time.Since(begin)), + slog.String("duration", time.Since(begin).String()), slog.String("thing_key", thingKey), slog.String("channel_id", chanID), } From 59679e8f2c75ea0dfb6e6cf5bfc59c2cec65356a Mon Sep 17 00:00:00 2001 From: Musilah Date: Fri, 19 Jan 2024 19:29:04 +0300 Subject: [PATCH 07/23] update slog arguments for remaining services Signed-off-by: Musilah --- auth/api/logging.go | 459 ++++++++++++----------------- bootstrap/api/logging.go | 271 +++++++---------- certs/api/logging.go | 121 ++++---- coap/api/logging.go | 71 ++--- consumers/notifiers/api/logging.go | 117 ++++---- internal/groups/api/logging.go | 272 ++++++++--------- lora/api/logging.go | 190 ++++++------ opcua/api/logging.go | 172 +++++------ provision/api/logging.go | 12 +- readers/api/logging.go | 4 +- things/api/logging.go | 60 ++-- twins/api/logging.go | 28 +- ws/api/logging.go | 4 +- 13 files changed, 754 insertions(+), 1027 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index e3b659fd0..e6b3ad426 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -28,19 +27,16 @@ func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service { func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := "Method list_objects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("limit", limit), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List objects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List objects completed successfully", args...) }(time.Now()) return lm.svc.ListObjects(ctx, pr, nextPageToken, limit) @@ -48,19 +44,15 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := "Method list_all_objects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List all objects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List all objects completed successfully", args...) }(time.Now()) return lm.svc.ListAllObjects(ctx, pr) @@ -68,38 +60,31 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - message := "Method count_objects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Count objects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Count objects completed successfully", args...) }(time.Now()) return lm.svc.CountObjects(ctx, pr) } func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := "Method list_subjects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("limit", limit), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List subjects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List subjects completed successfully", args...) }(time.Now()) return lm.svc.ListSubjects(ctx, pr, nextPageToken, limit) @@ -107,19 +92,15 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - message := "Method list_all_subjects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List all subjects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List all subjects completed successfully", args...) }(time.Now()) return lm.svc.ListAllSubjects(ctx, pr) @@ -127,38 +108,31 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - message := "Method count_subjects completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Count subjects failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Count subjects completed successfully", args...) }(time.Now()) return lm.svc.CountSubjects(ctx, pr) } func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.PolicyReq, filterPermissions []string) (p auth.Permissions, err error) { defer func(begin time.Time) { - message := "Method list_permissions completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Any("filter_permissions", filterPermissions), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List permissions failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List permissions completed successfully", args...) }(time.Now()) return lm.svc.ListPermissions(ctx, pr, filterPermissions) @@ -166,26 +140,18 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.Key) (tkn auth.Token, err error) { defer func(begin time.Time) { - d := "" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if key.Type != auth.AccessKey && !key.ExpiresAt.IsZero() { - d = fmt.Sprintf("with expiration date %v", key.ExpiresAt) + args = append(args, slog.Any("expiration_date", key.ExpiresAt)) } - message := fmt.Sprintf("Method issue for key %s completed", d) if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Issue failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("key_type", key.Type.String()), - slog.String("key_id", key.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Issue completed successfully", args...) }(time.Now()) return lm.svc.Issue(ctx, token, key) @@ -193,21 +159,16 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method revoke completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Revoke failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("key_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Revoke completed successfully", args...) }(time.Now()) return lm.svc.Revoke(ctx, token, id) @@ -215,21 +176,16 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) (key auth.Key, err error) { defer func(begin time.Time) { - message := "Method retrieve_key completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Retrieve key failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("key_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Retrieve key completed successfully", args...) }(time.Now()) return lm.svc.RetrieveKey(ctx, token, id) @@ -237,20 +193,15 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id auth.Key, err error) { defer func(begin time.Time) { - message := "Method identify completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Identify failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Identify completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, token) @@ -258,57 +209,45 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := "Method authorize completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Authorize failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Authorize completed successfully", args...) }(time.Now()) return lm.svc.Authorize(ctx, pr) } func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := "Method add_policy completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Add policy failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Add policy completed successfully", args...) }(time.Now()) return lm.svc.AddPolicy(ctx, pr) } func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := "Method add_policies completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Add policies failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Add policies completed successfully", args...) }(time.Now()) return lm.svc.AddPolicies(ctx, prs) @@ -316,222 +255,194 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := "Method delete_policy completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Delete policy failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Delete policy completed successfully", args...) }(time.Now()) return lm.svc.DeletePolicy(ctx, pr) } func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - message := "Method delete_policies completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Delete policies failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Delete policies completed successfully", args...) }(time.Now()) return lm.svc.DeletePolicies(ctx, prs) } func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d auth.Domain) (do auth.Domain, err error) { defer func(begin time.Time) { - message := "Method create_domain completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "domain", + slog.String("domain_id", d.ID), + slog.String("name", d.Name), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args := append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create domain failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create domain completed successfully", args...) }(time.Now()) return lm.svc.CreateDomain(ctx, token, d) } func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id string) (do auth.Domain, err error) { defer func(begin time.Time) { - message := "Method retrieve_domain completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("domain_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Retrieve domain failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("domain_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Retrieve domain completed successfully", args...) }(time.Now()) return lm.svc.RetrieveDomain(ctx, token, id) } func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, token, id string) (permissions auth.Permissions, err error) { defer func(begin time.Time) { - message := "Method retrieve_domain_permissions completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("domain_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Retrieve domain permissions failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("domain_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Retrieve domain permissions completed successfully", args...) }(time.Now()) return lm.svc.RetrieveDomainPermissions(ctx, token, id) } func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - message := "Method update_domain completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "domain", + slog.String("domain_id", id), + slog.Any("name", d.Name), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update domain failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("domain_id", id), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update domain completed successfully", args...) }(time.Now()) return lm.svc.UpdateDomain(ctx, token, id, d) } func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - message := "Method change_domain_status completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "domain", + slog.String("domain_id", id), + slog.Any("status", d.Status), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Change domain status failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("domain_id", "id"), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Change domain status completed successfully", args...) }(time.Now()) return lm.svc.ChangeDomainStatus(ctx, token, id, d) } func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - message := "Method list_domains completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "page", + slog.Any("limit", page.Limit), + slog.Any("offset", page.Offset), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List domains failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List domains completed successfully", args...) }(time.Now()) return lm.svc.ListDomains(ctx, token, page) } func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - message := "Method assign_users completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + slog.String("relation", relation), + slog.Any("user_ids", userIds), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Assign users failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Assign users completed successfully", args...) }(time.Now()) return lm.svc.AssignUsers(ctx, token, id, userIds, relation) } func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - message := "Method unassign_users completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + slog.String("relation", relation), + slog.Any("user_ids", userIds), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Unassign users failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("user_ids", fmt.Sprintf("%v", userIds)), - slog.String("relation", relation), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Unassign users completed successfully", args...) }(time.Now()) return lm.svc.UnassignUsers(ctx, token, id, userIds, relation) } func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - message := "Method list_user_domains completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("user_id", userID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List user domains failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("user_id", userID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List user domains completed successfully", args...) }(time.Now()) return lm.svc.ListUserDomains(ctx, token, userID, page) } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index b92180ec1..bbdc9d539 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -30,21 +29,16 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method add completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", saved.ThingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Add failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", saved.ThingID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Add completed successfully", args...) }(time.Now()) return lm.svc.Add(ctx, token, cfg) @@ -54,142 +48,115 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method view completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", saved.ThingID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View completed successfully", args...) }(time.Now()) return lm.svc.View(ctx, token, id) } -// Update logs the update request. It logs token, bootstrap thing ID and the time it took to complete the request. +// Update logs the update request. It logs bootstrap thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { - message := "Method update completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", cfg.ThingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", cfg.ThingID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update completed successfully", args...) }(time.Now()) return lm.svc.Update(ctx, token, cfg) } -// UpdateCert logs the update_cert request. It logs token, thing ID and the time it took to complete the request. +// UpdateCert logs the update_cert request. It logs thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method update_cert completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", cfg.ThingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update cert failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update cert completed successfully", args...) }(time.Now()) return lm.svc.UpdateCert(ctx, token, thingID, clientCert, clientKey, caCert) } -// UpdateConnections logs the update_connections request. It logs token, bootstrap ID and the time it took to complete the request. +// UpdateConnections logs the update_connections request. It logs bootstrap ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { - message := "Method update_connections completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update connections failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update connections completed successfully", args...) }(time.Now()) return lm.svc.UpdateConnections(ctx, token, id, connections) } -// List logs the list request. It logs token, offset, limit and the time it took to complete the request. +// List logs the list request. It logs offset, limit and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { - message := "Method list completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "filter", + slog.Uint64("offset", offset), + slog.Uint64("limit", limit), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List completed successfully", args...) }(time.Now()) return lm.svc.List(ctx, token, filter, offset, limit) } -// Remove logs the remove request. It logs token, bootstrap ID and the time it took to complete the request. +// Remove logs the remove request. It logs bootstrap ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method remove completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove completed successfully", args...) }(time.Now()) return lm.svc.Remove(ctx, token, id) @@ -197,21 +164,20 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - message := "Method bootstrap completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "config", + slog.String("external_id", externalID), + slog.String("external_key", externalKey), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Bootstrap failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("external_id", externalID), - slog.String("external_key", externalKey), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Bootstrap completed successfully", args...) }(time.Now()) return lm.svc.Bootstrap(ctx, externalKey, externalID, secure) @@ -219,22 +185,17 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { - message := "Method change_state completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + slog.Any("state", state), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Change state failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", id), - slog.String("token", token), - slog.String("state", state.String()), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Change state completed successfully", args...) }(time.Now()) return lm.svc.ChangeState(ctx, token, id, state) @@ -242,20 +203,21 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { - message := "Method update_channel_handler completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "channel", + slog.String("channel_id", channel.ID), + slog.String("channel_name", channel.Name), + slog.Any("channel_metadata", channel.Metadata), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update channel handler failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", channel.ID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update channel handler completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannelHandler(ctx, channel) @@ -263,20 +225,16 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_config_handler completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("config_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove config handler failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("config_id", id), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove config handler completed successfully", args...) }(time.Now()) return lm.svc.RemoveConfigHandler(ctx, id) @@ -284,20 +242,16 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_channel_handler completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove channel handler failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", id), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove channel handler completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannelHandler(ctx, id) @@ -305,21 +259,16 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { - message := "Method disconnect_thing_handler completed" + args := []interface{}{ + slog.String("channel_id", channelID), + slog.String("thing_id", thingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disconnect thing handler failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", channelID), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disconnect thing handler completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThingHandler(ctx, channelID, thingID) diff --git a/certs/api/logging.go b/certs/api/logging.go index 57a3a79c0..cd4af6b08 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -26,124 +25,106 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { return &loggingMiddleware{logger, svc} } -// IssueCert logs the issue_cert request. It logs the token, thing ID and the time it took to complete the request. +// IssueCert logs the issue_cert request. It logs the ttl, thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := "Method issue_cert completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + slog.String("ttl", ttl), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Issue cert failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("token", token), - slog.String("ttl", ttl), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Issue cert completed successfully", args...) }(time.Now()) return lm.svc.IssueCert(ctx, token, thingID, ttl) } -// ListCerts logs the list_certs request. It logs the token, thing ID and the time it took to complete the request. +// ListCerts logs the list_certs request. It logs the thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message := "Method list_certs completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + slog.Group( + "page", + slog.Uint64("offset", cp.Offset), + slog.Uint64("limit", cp.Limit), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List certs failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("token", token), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List certs completed successfully", args...) }(time.Now()) return lm.svc.ListCerts(ctx, token, thingID, offset, limit) } -// ListSerials logs the list_serials request. It logs the token, thing ID and the time it took to complete the request. +// ListSerials logs the list_serials request. It logs the thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - message := "Method list_serials completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + slog.Group( + "page", + slog.Uint64("offset", cp.Offset), + slog.Uint64("limit", cp.Limit), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List serials failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("thing_id", thingID), - slog.Uint64("offset", offset), - slog.Uint64("limit", limit), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List serials completed successfully", args...) }(time.Now()) return lm.svc.ListSerials(ctx, token, thingID, offset, limit) } -// ViewCert logs the view_cert request. It logs the token, serial ID and the time it took to complete the request. +// ViewCert logs the view_cert request. It logs the serial ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { - message := "Method view_cert completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("serial_id", serialID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View cert failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("serial_id", serialID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View cert completed successfully", args...) }(time.Now()) return lm.svc.ViewCert(ctx, token, serialID) } -// RevokeCert logs the revoke_cert request. It logs the token, thing ID and the time it took to complete the request. +// RevokeCert logs the revoke_cert request. It logs the thing ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { - message := "Method revoke_cert completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Revoke cert failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Revoke cert completed successfully", args...) }(time.Now()) return lm.svc.RevokeCert(ctx, token, thingID) diff --git a/coap/api/logging.go b/coap/api/logging.go index 7432c3a19..1464c5f25 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -31,26 +30,20 @@ func LoggingMiddleware(svc coap.Service, logger *slog.Logger) coap.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messaging.Message) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("key", key), + } destChannel := msg.GetChannel() if msg.GetSubtopic() != "" { - destChannel = fmt.Sprintf("%s.%s", destChannel, msg.GetSubtopic()) + args = append(args, slog.String("channel", destChannel), slog.String("subtopic", msg.GetSubtopic())) } - message := "Method publish completed" if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "publish"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Publish failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "publish"), - slog.String("channel", destChannel), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Publish completed successfully", args...) }(time.Now()) return lm.svc.Publish(ctx, key, msg) @@ -60,28 +53,20 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa // If the request fails, it logs the error. func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopic string, c coap.Client) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("key", key), + } destChannel := chanID if subtopic != "" { - destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) + args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) } - message := "Method subscribe completed" if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("method", "subscribe"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Subscribe failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "subscribe"), - slog.String("channel", destChannel), - slog.String("client", c.Token()), - slog.String("channelID", chanID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Subscribe completed successfully", args...) }(time.Now()) return lm.svc.Subscribe(ctx, key, chanID, subtopic, c) @@ -91,28 +76,20 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi // If the request fails, it logs the error. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) (err error) { defer func(begin time.Time) { + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("key", key), + } destChannel := chanID if subtopic != "" { - destChannel = fmt.Sprintf("%s.%s", destChannel, subtopic) + args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) } - message := "Method unsubscribe completed" if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error: %s.", message, err), - slog.String("method", "unsubscribe"), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Unsubscribe failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("method", "unsubscribe"), - slog.String("channel", destChannel), - slog.String("token", token), - slog.String("key", key), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Unsubscribe completed successfully", args...) }(time.Now()) return lm.svc.Unsubscribe(ctx, key, chanID, subtopic, token) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 27f8e970d..fb6c5bce0 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -26,120 +25,108 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser return &loggingMiddleware{logger, svc} } -// CreateSubscription logs the create_subscription request. It logs token and subscription ID and the time it took to complete the request. +// CreateSubscription logs the create_subscription request. It logs subscription ID and topic and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { - message := "Method create_subscription completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "subscription", + slog.String("topic", sub.Topic), + slog.String("id", id), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create subscription failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create subscription completed successfully", args...) }(time.Now()) return lm.svc.CreateSubscription(ctx, token, sub) } -// ViewSubscription logs the view_subscription request. It logs token and subscription topic and the time it took to complete the request. +// ViewSubscription logs the view_subscription request. It logs subscription topic and id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { - message := "Method view_subscription completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "subscription", + slog.String("topic", sub.Topic), + slog.String("id", sub.ID), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View subscription failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("topic", topic), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View subscription completed successfully", args...) }(time.Now()) return lm.svc.ViewSubscription(ctx, token, topic) } -// ListSubscriptions logs the list_subscriptions request. It logs token and subscription topic and the time it took to complete the request. +// ListSubscriptions logs the list_subscriptions request. It logs page metadata and subscription topic and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { - message := "Method list_subscriptions completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "page_metadata", + slog.String("topic", pm.Topic), + slog.Any("limit", pm.Limit), + slog.Any("offset", pm.Offset), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List subscriptions failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("topic", pm.Topic), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List subscriptions completed successfully", args...) }(time.Now()) return lm.svc.ListSubscriptions(ctx, token, pm) } -// RemoveSubscription logs the remove_subscription request. It logs token and subscription ID and the time it took to complete the request. +// RemoveSubscription logs the remove_subscription request. It logs subscription ID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method remove_subscription completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove subscription failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove subscription completed successfully", args...) }(time.Now()) return lm.svc.RemoveSubscription(ctx, token, id) } -// ConsumeBlocking logs the consume_blocking request. It logs the message and the time it took to complete the request. +// ConsumeBlocking logs the consume_blocking request. It logs the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { - message := "Method consume_blocking completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Consume blocking failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("message", fmt.Sprintf("%v", msg)), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Consume blocking completed successfully", args...) }(time.Now()) return lm.svc.ConsumeBlocking(ctx, msg) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 69fadb77a..9157083db 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -5,7 +5,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -28,213 +27,192 @@ func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method create_group %s completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("group_name", g.Name), + slog.String("kind", kind), + slog.String("group_id", g.ID), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_name", g.Name), - slog.String("token", token), - slog.String("kind", kind), - slog.String("group_id", g.ID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create group completed successfully", args...) }(time.Now()) return lm.svc.CreateGroup(ctx, token, kind, group) } -// UpdateGroup logs the update_group request. It logs the group name, id and token and the time it took to complete the request. +// UpdateGroup logs the update_group request. It logs the group name, id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method update_group completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("group_name", g.Name), + slog.String("group_id", g.ID), + slog.Any("metadata", g.Metadata), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_name", g.Name), - slog.String("group_id", g.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update group completed successfully", args...) }(time.Now()) return lm.svc.UpdateGroup(ctx, token, group) } -// ViewGroup logs the view_group request. It logs the group name, id and token and the time it took to complete the request. +// ViewGroup logs the view_group request. It logs the group name, id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method view_group completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("group_id", g.ID), + slog.String("group_name", g.Name), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_name", g.Name), - slog.String("group_id", g.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View group completed successfully", args...) }(time.Now()) return lm.svc.ViewGroup(ctx, token, id) } -// ViewGroupPerms logs the view_group request. It logs the group name, id and token and the time it took to complete the request. +// ViewGroupPerms logs the view_group request. It logs the group id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - message := "Method view_group_perms completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("group_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("View group permissions failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("View group permissions completed successfully", args...) }(time.Now()) return lm.svc.ViewGroupPerms(ctx, token, id) } -// ListGroups logs the list_groups request. It logs the token and the time it took to complete the request. +// ListGroups logs the list_groups request. It logs the page metadata and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, memberID string, gp groups.Page) (cg groups.Page, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_groups %d groups completed", cg.Total) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("member_kind", memberKind), + slog.String("member_id", memberID), + slog.Group( + "page", + slog.Any("limit", gp.Limit), + slog.Any("offset", gp.Offset)), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List groups failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List groups completed successfully", args...) }(time.Now()) return lm.svc.ListGroups(ctx, token, memberKind, memberID, gp) } -// EnableGroup logs the enable_group request. It logs the group name, id and token and the time it took to complete the request. +// EnableGroup logs the enable_group request. It logs the group name, id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method enable_group completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("group_id", id), + slog.String("group_name", g.Name), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Enable group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_name", g.Name), - slog.String("group_id", g.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Enable group completed successfully", args...) }(time.Now()) return lm.svc.EnableGroup(ctx, token, id) } -// DisableGroup logs the disable_group request. It logs the group name, id and token and the time it took to complete the request. +// DisableGroup logs the disable_group request. It logs the group id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - message := "Method disable_group completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("group_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disable group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_name", g.Name), - slog.String("group_id", g.ID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disable group completed successfully", args...) }(time.Now()) return lm.svc.DisableGroup(ctx, token, id) } -// ListMembers logs the list_members request. It logs the groupID and token and the time it took to complete the request. +// ListMembers logs the list_members request. It logs the groupID and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (mp groups.MembersPage, err error) { defer func(begin time.Time) { - message := "Method list_members completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "group", + slog.String("group_id", groupID), + slog.String("permission", permission), + slog.String("member_kind", memberKind), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("List members failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_id", groupID), - slog.String("token", token), - slog.String("permission", permission), - slog.String("member_kind", memberKind), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("List members completed successfully", args...) }(time.Now()) return lm.svc.ListMembers(ctx, token, groupID, permission, memberKind) } func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := "Method assign completed" - if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) - return - } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), slog.String("group_id", groupID), - slog.String("token", token), slog.String("relation", relation), slog.String("member_kind", memberKind), - slog.String("duration", time.Since(begin).String()), - ) + slog.Any("member_ids", memberIDs), + } + if err != nil { + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Assign failed to complete successfully", args...) + return + } + lm.logger.Info("Assign completed successfully", args...) }(time.Now()) return lm.svc.Assign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -242,22 +220,19 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - message := "Method unassign completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("group_id", groupID), + slog.String("relation", relation), + slog.String("member_kind", memberKind), + slog.Any("member_ids", memberIDs), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Unassign failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("member_IDS", fmt.Sprintf("%v", memberIDs)), - slog.String("group_id", groupID), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Unassign completed successfully", args...) }(time.Now()) return lm.svc.Unassign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -265,21 +240,16 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - message := "Method delete_group completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("group_id", id), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Delete group failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("group_id", id), - slog.String("token", token), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Delete group completed successfully", args...) }(time.Now()) return lm.svc.DeleteGroup(ctx, token, id) } diff --git a/lora/api/logging.go b/lora/api/logging.go index 0b92fdc0c..eb5bada83 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -31,21 +30,20 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := "Method create_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "thing", + slog.String("thing_id", thingID), + slog.String("lora_dev_eui", loraDevEUI), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("lora_dev_eui", loraDevEUI), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create thing completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, thingID, loraDevEUI) @@ -53,21 +51,20 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - message := "Method update_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "thing", + slog.String("thing_id", thingID), + slog.String("lora_dev_eui", loraDevEUI), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("lora_dev_eui", loraDevEUI), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update thing completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, thingID, loraDevEUI) @@ -75,20 +72,16 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { - message := "Method remove_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", thingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove thing completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, thingID) @@ -96,21 +89,20 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := "Method create_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "channel", + slog.String("channel_id", chanID), + slog.String("lora_app", loraApp), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", chanID), - slog.String("lora_app", loraApp), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create channel completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, chanID, loraApp) @@ -118,21 +110,19 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - message := "Method update_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "channel", + slog.String("channel_id", chanID), + slog.String("lora_app", loraApp), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Warn("Update channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", chanID), - slog.String("lora_app", loraApp), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update channel completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, chanID, loraApp) @@ -140,20 +130,15 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { - message := "Method remove_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", chanID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Warn("Remove channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", chanID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove channel completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, chanID) @@ -161,21 +146,17 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method connect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", chanID), + slog.String("thing_id", thingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args := append(args, slog.String("error", err.Error())) + lm.logger.Warn("Connect thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", chanID), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Connect thing completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, chanID, thingID) @@ -183,21 +164,17 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method disconnect_thing mgx-%s : mgx-%s, took %s to complete", chanID, thingID, time.Since(begin)) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", chanID), + slog.String("thing_id", thingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args := append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disconnect thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("channel_id", chanID), - slog.String("thing_id", thingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disconnect thing completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, chanID, thingID) @@ -205,21 +182,20 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method publish application/%s/device/%s/rx took %s to complete", msg.ApplicationID, msg.DevEUI, time.Since(begin)) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "message", + slog.String("application_id", msg.ApplicationID), + slog.String("device_eui", msg.DevEUI), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error.", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Publish failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors.", message), - slog.String("application_id", msg.ApplicationID), - slog.String("device_eui", msg.DevEUI), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Publish completed successfully", args...) }(time.Now()) return lm.svc.Publish(ctx, msg) diff --git a/opcua/api/logging.go b/opcua/api/logging.go index d66190c40..c4a8a492a 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -41,10 +40,10 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Create thing failed to complete successfully.", args...) + lm.logger.Warn("Create thing failed to complete successfully", args...) return } - lm.logger.Info("Create thing completed successfully.", args...) + lm.logger.Info("Create thing completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) @@ -52,21 +51,20 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - message := "Method update_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "thing", + slog.String("thing_id", mgxThing), + slog.String("opcua_node_id", opcuaNodeID), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("thing_id", mgxThing), - slog.String("opcua_node_id", opcuaNodeID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update thing completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, mgxThing, opcuaNodeID) @@ -74,20 +72,16 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { - message := "Method remove_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("thing_id", mgxThing), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("thing_id", mgxThing), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove thing completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, mgxThing) @@ -95,21 +89,20 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := "Method create_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "channel", + slog.String("channel_id", mgxChan), + slog.String("opcua_server_uri", opcuaServerURI), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Create channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_id", mgxChan), - slog.String("opcua_server_uri", opcuaServerURI), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Create channel completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, mgxChan, opcuaServerURI) @@ -117,21 +110,20 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { - message := "Method update_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.Group( + "channel", + slog.String("channel_id", mgxChanID), + slog.String("opcua_server_uri", opcuaServerURI), + ), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Update channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_id", mgxChanID), - slog.String("opcua_server_uri", opcuaServerURI), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Update channel completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, mgxChanID, opcuaServerURI) @@ -139,20 +131,16 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { - message := "Method remove_channel completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", mgxChanID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Remove channel failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_id", mgxChanID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Remove channel completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, mgxChanID) @@ -160,21 +148,17 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := "Method connect_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", mgxChanID), + slog.String("thing_id", mgxThingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Connect thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_id", mgxChanID), - slog.String("thing_id", mgxThingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Connect thing completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, mgxChanID, mgxThingID) @@ -182,21 +166,17 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - message := "Method disconnect_thing completed" + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", mgxChanID), + slog.String("thing_id", mgxThingID), + } if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Disconnect thing failed to complete successfully", args...) return } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), - slog.String("channel_id", mgxChanID), - slog.String("thing_id", mgxThingID), - slog.String("duration", time.Since(begin).String()), - ) + lm.logger.Info("Disconnect thing completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, mgxChanID, mgxThingID) @@ -204,22 +184,18 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, identifier string) (nodes []opcua.BrowsedNode, err error) { defer func(begin time.Time) { - message := "Method browse completed" - if err != nil { - lm.logger.Warn( - fmt.Sprintf("%s with error", message), - slog.String("error", err.Error()), - slog.String("duration", time.Since(begin).String()), - ) - return - } - lm.logger.Info( - fmt.Sprintf("%s without errors", message), + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), slog.String("server_uri", serverURI), slog.String("namespace", namespace), slog.String("identifier", identifier), - slog.String("duration", time.Since(begin).String()), - ) + } + if err != nil { + args = append(args, slog.String("error", err.Error())) + lm.logger.Warn("Browse failed to complete successfully", args...) + return + } + lm.logger.Info("Browse completed successfully", args...) }(time.Now()) return lm.svc.Browse(ctx, serverURI, namespace, identifier) diff --git a/provision/api/logging.go b/provision/api/logging.go index 1c9c0a645..2cbe152cf 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -34,10 +34,10 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Provision failed to complete successfully.", args...) + lm.logger.Warn("Provision failed to complete successfully", args...) return } - lm.logger.Info("Provision completed successfully.", args...) + lm.logger.Info("Provision completed successfully", args...) }(time.Now()) return lm.svc.Provision(token, name, externalID, externalKey) @@ -52,10 +52,10 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Cert failed to complete successfully.", args...) + lm.logger.Warn("Cert failed to complete successfully", args...) return } - lm.logger.Info("Cert completed successfully.", args...) + lm.logger.Info("Cert completed successfully", args...) }(time.Now()) return lm.svc.Cert(token, thingID, duration) @@ -68,10 +68,10 @@ func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Mapping failed to complete successfully.", args...) + lm.logger.Warn("Mapping failed to complete successfully", args...) return } - lm.logger.Info("Mapping completed successfully.", args...) + lm.logger.Info("Mapping completed successfully", args...) }(time.Now()) return lm.svc.Mapping(token) diff --git a/readers/api/logging.go b/readers/api/logging.go index a04d5145d..65a87c2ae 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -42,10 +42,10 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Read all failed to complete successfully.", args...) + lm.logger.Warn("Read all failed to complete successfully", args...) return } - lm.logger.Info("Read all completed successfully.", args...) + lm.logger.Info("Read all completed successfully", args...) }(time.Now()) return lm.svc.ReadAll(chanID, rpm) diff --git a/things/api/logging.go b/things/api/logging.go index 777992be6..f8d20b065 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -31,10 +31,10 @@ func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, cli } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Create thing failed to complete successfully.", args...) + lm.logger.Warn("Create thing failed to complete successfully", args...) return } - lm.logger.Info("Create thing completed successfully.", args...) + lm.logger.Info("Create thing completed successfully", args...) }(time.Now()) return lm.svc.CreateThings(ctx, token, clients...) } @@ -47,10 +47,10 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("View client failed to complete successfully.", args...) + lm.logger.Warn("View client failed to complete successfully", args...) return } - lm.logger.Info("View client completed successfully.", args...) + lm.logger.Info("View client completed successfully", args...) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } @@ -63,10 +63,10 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("View client permissions failed to complete successfully.", args...) + lm.logger.Warn("View client permissions failed to complete successfully", args...) return } - lm.logger.Info("View client permissions completed successfully.", args...) + lm.logger.Info("View client permissions completed successfully", args...) }(time.Now()) return lm.svc.ViewClientPerms(ctx, token, id) } @@ -84,10 +84,10 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List clients failed to complete successfully.", args...) + lm.logger.Warn("List clients failed to complete successfully", args...) return } - lm.logger.Info("List clients completed successfully.", args...) + lm.logger.Info("List clients completed successfully", args...) }(time.Now()) return lm.svc.ListClients(ctx, token, reqUserID, pm) } @@ -105,10 +105,10 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client failed to complete successfully.", args...) + lm.logger.Warn("Update client failed to complete successfully", args...) return } - lm.logger.Info("Update client completed successfully.", args...) + lm.logger.Info("Update client completed successfully", args...) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } @@ -125,10 +125,10 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client tags failed to complete successfully.", args...) + lm.logger.Warn("Update client tags failed to complete successfully", args...) return } - lm.logger.Info("Update client tags completed successfully.", args...) + lm.logger.Info("Update client tags completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } @@ -144,10 +144,10 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client secret failed to complete successfully.", args...) + lm.logger.Warn("Update client secret failed to complete successfully", args...) return } - lm.logger.Info("Update client secret completed successfully.", args...) + lm.logger.Info("Update client secret completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } @@ -160,10 +160,10 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Enable client failed to complete successfully.", args...) + lm.logger.Warn("Enable client failed to complete successfully", args...) return } - lm.logger.Info("Enable client completed successfully.", args...) + lm.logger.Info("Enable client completed successfully", args...) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } @@ -176,10 +176,10 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Disable client failed to complete successfully.", args...) + lm.logger.Warn("Disable client failed to complete successfully", args...) return } - lm.logger.Info("Disable client completed successfully.", args...) + lm.logger.Info("Disable client completed successfully", args...) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } @@ -197,10 +197,10 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List clients by group failed to complete successfully.", args...) + lm.logger.Warn("List clients by group failed to complete successfully", args...) return } - lm.logger.Info("List clients by group completed successfully.", args...) + lm.logger.Info("List clients by group completed successfully", args...) }(time.Now()) return lm.svc.ListClientsByGroup(ctx, token, channelID, cp) } @@ -217,10 +217,10 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Identify failed to complete successfully.", args...) + lm.logger.Warn("Identify failed to complete successfully", args...) return } - lm.logger.Info("Identify completed successfully.", args...) + lm.logger.Info("Identify completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, key) } @@ -234,10 +234,10 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Authorize failed to complete successfully.", args...) + lm.logger.Warn("Authorize failed to complete successfully", args...) return } - lm.logger.Info("Authorize completed successfully.", args...) + lm.logger.Info("Authorize completed successfully", args...) }(time.Now()) return lm.svc.Authorize(ctx, req) } @@ -252,10 +252,10 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Share failed to complete successfully.", args...) + lm.logger.Warn("Share failed to complete successfully", args...) return } - lm.logger.Info("Share completed successfully.", args...) + lm.logger.Info("Share completed successfully", args...) }(time.Now()) return lm.svc.Share(ctx, token, id, relation, userids...) } @@ -270,10 +270,10 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Unshare failed to complete successfully.", args...) + lm.logger.Warn("Unshare failed to complete successfully", args...) return } - lm.logger.Info("Unshare completed successfully.", args...) + lm.logger.Info("Unshare completed successfully", args...) }(time.Now()) return lm.svc.Unshare(ctx, token, id, relation, userids...) } @@ -286,10 +286,10 @@ func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Delete client failed to complete successfully.", args...) + lm.logger.Warn("Delete client failed to complete successfully", args...) return } - lm.logger.Info("Delete client completed successfully.", args...) + lm.logger.Info("Delete client completed successfully", args...) }(time.Now()) return lm.svc.DeleteClient(ctx, token, id) } diff --git a/twins/api/logging.go b/twins/api/logging.go index e8c2cac50..eb9c41988 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -39,10 +39,10 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Add twin failed to complete successfully.", args...) + lm.logger.Warn("Add twin failed to complete successfully", args...) return } - lm.logger.Info("Add twin completed successfully.", args...) + lm.logger.Info("Add twin completed successfully", args...) }(time.Now()) return lm.svc.AddTwin(ctx, token, twin, def) @@ -61,10 +61,10 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update twin failed to complete successfully.", args...) + lm.logger.Warn("Update twin failed to complete successfully", args...) return } - lm.logger.Info("Update twin completed successfully.", args...) + lm.logger.Info("Update twin completed successfully", args...) }(time.Now()) return lm.svc.UpdateTwin(ctx, token, twin, def) @@ -78,10 +78,10 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("View twin failed to complete successfully.", args...) + lm.logger.Warn("View twin failed to complete successfully", args...) return } - lm.logger.Info("View twin completed successfully.", args...) + lm.logger.Info("View twin completed successfully", args...) }(time.Now()) return lm.svc.ViewTwin(ctx, token, twinID) @@ -100,10 +100,10 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List twins failed to complete successfully.", args...) + lm.logger.Warn("List twins failed to complete successfully", args...) return } - lm.logger.Info("List twins completed successfully.", args...) + lm.logger.Info("List twins completed successfully", args...) }(time.Now()) return lm.svc.ListTwins(ctx, token, offset, limit, name, metadata) @@ -122,10 +122,10 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Save states failed to complete successfully.", args...) + lm.logger.Warn("Save states failed to complete successfully", args...) return } - lm.logger.Info("Save states completed successfully.", args...) + lm.logger.Info("Save states completed successfully", args...) }(time.Now()) return lm.svc.SaveStates(ctx, msg) @@ -144,10 +144,10 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("List states failed to complete successfully.", args...) + lm.logger.Warn("List states failed to complete successfully", args...) return } - lm.logger.Info("List states completed successfully.", args...) + lm.logger.Info("List states completed successfully", args...) }(time.Now()) return lm.svc.ListStates(ctx, token, offset, limit, twinID) @@ -161,10 +161,10 @@ func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID strin } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Remove twin failed to complete successfully.", args...) + lm.logger.Warn("Remove twin failed to complete successfully", args...) return } - lm.logger.Info("Remove twin completed successfully.", args...) + lm.logger.Info("Remove twin completed successfully", args...) }(time.Now()) return lm.svc.RemoveTwin(ctx, token, twinID) diff --git a/ws/api/logging.go b/ws/api/logging.go index 44b56e4f4..075ee6bf5 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -38,10 +38,10 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, su } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Warn("Subscibe failed to complete successfully.", args...) + lm.logger.Warn("Subscibe failed to complete successfully", args...) return } - lm.logger.Info("Subscribe completed successfully.", args...) + lm.logger.Info("Subscribe completed successfully", args...) }(time.Now()) return lm.svc.Subscribe(ctx, thingKey, chanID, subtopic, c) From 690d7d98221b7b2f5bd58e736080d93bfe3e0fb2 Mon Sep 17 00:00:00 2001 From: Musilah Date: Fri, 19 Jan 2024 19:35:43 +0300 Subject: [PATCH 08/23] update users go docs Signed-off-by: Musilah --- users/api/logging.go | 48 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/users/api/logging.go b/users/api/logging.go index f0bc9e8f2..53b7aa1fa 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -26,7 +26,7 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { return &loggingMiddleware{logger, svc} } -// RegisterClient logs the register_client request. It logs the client id and token and the time it took to complete the request. +// RegisterClient logs the register_client request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -44,7 +44,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c return lm.svc.RegisterClient(ctx, token, client) } -// IssueToken logs the issue_token request. It logs the client identity and token type and the time it took to complete the request. +// IssueToken logs the issue_token request. It logs the client identity type and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { @@ -86,7 +86,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom return lm.svc.RefreshToken(ctx, refreshToken, domainID) } -// ViewClient logs the view_client request. It logs the client id and token and the time it took to complete the request. +// ViewClient logs the view_client request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -104,7 +104,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( return lm.svc.ViewClient(ctx, token, id) } -// ViewProfile logs the view_profile request. It logs the client id and token and the time it took to complete the request. +// ViewProfile logs the view_profile request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -122,13 +122,17 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m return lm.svc.ViewProfile(ctx, token) } -// ListClients logs the list_clients request. It logs the token and page metadata and the time it took to complete the request. +// ListClients logs the list_clients request. It logs the page metadata and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Any("number_of_clients", pm.Total), + slog.Group( + "page", + slog.Any("limit", pm.Limit), + slog.Any("offset", pm.Offset), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -140,7 +144,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m return lm.svc.ListClients(ctx, token, pm) } -// UpdateClient logs the update_client request. It logs the client id and token and the time it took to complete the request. +// UpdateClient logs the update_client request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -158,7 +162,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli return lm.svc.UpdateClient(ctx, token, client) } -// UpdateClientTags logs the update_client_tags request. It logs the client id and token and the time it took to complete the request. +// UpdateClientTags logs the update_client_tags request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -176,7 +180,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, return lm.svc.UpdateClientTags(ctx, token, client) } -// UpdateClientIdentity logs the update_identity request. It logs the client id and token and the time it took to complete the request. +// UpdateClientIdentity logs the update_identity request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -194,7 +198,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id return lm.svc.UpdateClientIdentity(ctx, token, id, identity) } -// UpdateClientSecret logs the update_client_secret request. It logs the client id and token and the time it took to complete the request. +// UpdateClientSecret logs the update_client_secret request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -212,7 +216,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } -// GenerateResetToken logs the generate_reset_token request. It logs the email and host and the time it took to complete the request. +// GenerateResetToken logs the generate_reset_token request. It logs the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { @@ -229,7 +233,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host return lm.svc.GenerateResetToken(ctx, email, host) } -// ResetSecret logs the reset_secret request. It logs the token and the time it took to complete the request. +// ResetSecret logs the reset_secret request. It logs the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { @@ -246,7 +250,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri return lm.svc.ResetSecret(ctx, token, secret) } -// SendPasswordReset logs the send_password_reset request. It logs the token and the time it took to complete the request. +// SendPasswordReset logs the send_password_reset request. It logs the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { @@ -263,7 +267,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, return lm.svc.SendPasswordReset(ctx, host, email, user, token) } -// UpdateClientRole logs the update_client_role request. It logs the client id and token and the time it took to complete the request. +// UpdateClientRole logs the update_client_role request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -281,7 +285,7 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, return lm.svc.UpdateClientRole(ctx, token, client) } -// EnableClient logs the enable_client request. It logs the client id and token and the time it took to complete the request. +// EnableClient logs the enable_client request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -299,7 +303,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) return lm.svc.EnableClient(ctx, token, id) } -// DisableClient logs the disable_client request. It logs the client id and token and the time it took to complete the request. +// DisableClient logs the disable_client request. It logs the client id and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { @@ -317,13 +321,19 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string return lm.svc.DisableClient(ctx, token, id) } -// ListMembers logs the list_members request. It logs the group id, token and the time it took to complete the request. +// ListMembers logs the list_members request. It logs the group id, and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Any("total", mp.Total), + slog.String("object_kind", objectKind), + slog.String("object_id", objectID), + slog.Group( + "page", + slog.Any("limit", cp.Limit), + slog.Any("offset", cp.Offset), + ), } if err != nil { args = append(args, slog.String("error", err.Error())) @@ -335,7 +345,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, return lm.svc.ListMembers(ctx, token, objectKind, objectID, cp) } -// Identify logs the identify request. It logs the token and the time it took to complete the request. +// Identify logs the identify request. It logs the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { args := []interface{}{ From 60804091b8126b069706b6d26b65c6647ed267e1 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 11:09:19 +0300 Subject: [PATCH 09/23] return Warn level Signed-off-by: Musilah --- users/api/logging.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/users/api/logging.go b/users/api/logging.go index 53b7aa1fa..3ea97912c 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -36,7 +36,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Register client failed to complete successfully", args...) + lm.logger.Warn("Register client failed to complete successfully", args...) return } lm.logger.Info("Register client completed successfully", args...) @@ -57,7 +57,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Issue token failed to complete successfully", args...) + lm.logger.Warn("Issue token failed to complete successfully", args...) return } lm.logger.Info("Issue token completed successfully", args...) @@ -78,7 +78,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Refresh token failed to complete successfully", args...) + lm.logger.Warn("Refresh token failed to complete successfully", args...) return } lm.logger.Info("Refresh token completed successfully", args...) @@ -96,7 +96,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("View client failed to complete successfully", args...) + lm.logger.Warn("View client failed to complete successfully", args...) return } lm.logger.Info("View client completed successfully", args...) @@ -114,7 +114,7 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("View profile failed to complete successfully", args...) + lm.logger.Warn("View profile failed to complete successfully", args...) return } lm.logger.Info("View profile completed successfully", args...) @@ -136,7 +136,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("List clients failed to complete successfully", args...) + lm.logger.Warn("List clients failed to complete successfully", args...) return } lm.logger.Info("List clients completed successfully", args...) @@ -154,7 +154,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Update client failed to complete successfully", args...) + lm.logger.Warn("Update client failed to complete successfully", args...) return } lm.logger.Info("Update client completed successfully", args...) @@ -172,7 +172,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Update client tags failed to complete successfully", args...) + lm.logger.Warn("Update client tags failed to complete successfully", args...) return } lm.logger.Info("Update client tags completed successfully", args...) @@ -190,7 +190,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Update client identity failed to complete successfully", args...) + lm.logger.Warn("Update client identity failed to complete successfully", args...) return } lm.logger.Info("Update client identity completed successfully", args...) @@ -208,7 +208,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Update client secret failed to complete successfully", args...) + lm.logger.Warn("Update client secret failed to complete successfully", args...) return } lm.logger.Info("Update client secret completed successfully", args...) @@ -225,7 +225,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Generate reset token failed to complete successfully", args...) + lm.logger.Warn("Generate reset token failed to complete successfully", args...) return } lm.logger.Info("Generate reset token completed successfully", args...) @@ -242,7 +242,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Reset secret failed to complete successfully", args...) + lm.logger.Warn("Reset secret failed to complete successfully", args...) return } lm.logger.Info("Reset secret completed successfully", args...) @@ -259,7 +259,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Send password reset failed to complete successfully", args...) + lm.logger.Warn("Send password reset failed to complete successfully", args...) return } lm.logger.Info("Send password reset completed successfully", args...) @@ -277,7 +277,7 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Update client role failed to complete successfully", args...) + lm.logger.Warn("Update client role failed to complete successfully", args...) return } lm.logger.Info("Update client role completed successfully", args...) @@ -295,7 +295,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Enable client failed to complete successfully", args...) + lm.logger.Warn("Enable client failed to complete successfully", args...) return } lm.logger.Info("Enable client completed successfully", args...) @@ -313,7 +313,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Disable client failed to complete successfully", args...) + lm.logger.Warn("Disable client failed to complete successfully", args...) return } lm.logger.Info("Disable client completed successfully", args...) @@ -337,7 +337,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("List members failed to complete successfully", args...) + lm.logger.Warn("List members failed to complete successfully", args...) return } lm.logger.Info("List members completed successfully", args...) @@ -353,7 +353,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id str } if err != nil { args = append(args, slog.String("error", err.Error())) - lm.logger.Error("Identify failed to complete successfully", args...) + lm.logger.Warn("Identify failed to complete successfully", args...) return } lm.logger.Info("Identify completed successfully", args...) From d39b2e6aec7c1dd7a055c0b53e9ba3eeabfee0b7 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 14:06:24 +0300 Subject: [PATCH 10/23] fix error log Signed-off-by: Musilah --- auth/api/logging.go | 48 +++++++++++++++--------------- bootstrap/api/logging.go | 26 ++++++++-------- certs/api/logging.go | 10 +++---- coap/api/logging.go | 6 ++-- consumers/notifiers/api/logging.go | 10 +++---- internal/groups/api/logging.go | 22 +++++++------- lora/api/logging.go | 10 +++---- opcua/api/logging.go | 18 +++++------ pkg/messaging/handler/logging.go | 16 +++++----- provision/api/logging.go | 6 ++-- readers/api/logging.go | 2 +- things/api/logging.go | 28 ++++++++--------- twins/api/logging.go | 14 ++++----- users/api/logging.go | 36 +++++++++++----------- ws/api/logging.go | 2 +- 15 files changed, 127 insertions(+), 127 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index e6b3ad426..377174465 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -32,7 +32,7 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, slog.Any("limit", limit), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List objects failed to complete successfully", args...) return } @@ -48,7 +48,7 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List all objects failed to complete successfully", args...) return } @@ -64,7 +64,7 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Count objects failed to complete successfully", args...) return } @@ -80,7 +80,7 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq slog.Any("limit", limit), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List subjects failed to complete successfully", args...) return } @@ -96,7 +96,7 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List all subjects failed to complete successfully", args...) return } @@ -112,7 +112,7 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Count subjects failed to complete successfully", args...) return } @@ -128,7 +128,7 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy slog.Any("filter_permissions", filterPermissions), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List permissions failed to complete successfully", args...) return } @@ -147,7 +147,7 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K args = append(args, slog.Any("expiration_date", key.ExpiresAt)) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Issue failed to complete successfully", args...) return } @@ -164,7 +164,7 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Revoke failed to complete successfully", args...) return } @@ -181,7 +181,7 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Retrieve key failed to complete successfully", args...) return } @@ -197,7 +197,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Identify failed to complete successfully", args...) return } @@ -213,7 +213,7 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Authorize failed to complete successfully", args...) return } @@ -228,7 +228,7 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Add policy failed to complete successfully", args...) return } @@ -243,7 +243,7 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Add policies failed to complete successfully", args...) return } @@ -259,7 +259,7 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Delete policy failed to complete successfully", args...) return } @@ -274,7 +274,7 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Delete policies failed to complete successfully", args...) return } @@ -310,7 +310,7 @@ func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id strin slog.String("domain_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Retrieve domain failed to complete successfully", args...) return } @@ -326,7 +326,7 @@ func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, toke slog.String("domain_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Retrieve domain permissions failed to complete successfully", args...) return } @@ -346,7 +346,7 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update domain failed to complete successfully", args...) return } @@ -366,7 +366,7 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Change domain status failed to complete successfully", args...) return } @@ -386,7 +386,7 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List domains failed to complete successfully", args...) return } @@ -404,7 +404,7 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, slog.Any("user_ids", userIds), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Assign users failed to complete successfully", args...) return } @@ -422,7 +422,7 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string slog.Any("user_ids", userIds), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Unassign users failed to complete successfully", args...) return } @@ -438,7 +438,7 @@ func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID slog.String("user_id", userID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List user domains failed to complete successfully", args...) return } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index bbdc9d539..27935d7cb 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -34,7 +34,7 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra slog.String("thing_id", saved.ThingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Add failed to complete successfully", args...) return } @@ -53,7 +53,7 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved slog.String("thing_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View failed to complete successfully", args...) return } @@ -72,7 +72,7 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots slog.String("thing_id", cfg.ThingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update failed to complete successfully", args...) return } @@ -91,7 +91,7 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli slog.String("thing_id", cfg.ThingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update cert failed to complete successfully", args...) return } @@ -110,7 +110,7 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update connections failed to complete successfully", args...) return } @@ -133,7 +133,7 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List failed to complete successfully", args...) return } @@ -152,7 +152,7 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove failed to complete successfully", args...) return } @@ -173,7 +173,7 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Bootstrap failed to complete successfully", args...) return } @@ -191,7 +191,7 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, slog.Any("state", state), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Change state failed to complete successfully", args...) return } @@ -213,7 +213,7 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update channel handler failed to complete successfully", args...) return } @@ -230,7 +230,7 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) slog.String("config_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove config handler failed to complete successfully", args...) return } @@ -247,7 +247,7 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove channel handler failed to complete successfully", args...) return } @@ -264,7 +264,7 @@ func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channel slog.String("thing_id", thingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disconnect thing handler failed to complete successfully", args...) return } diff --git a/certs/api/logging.go b/certs/api/logging.go index cd4af6b08..278d3bef3 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -35,7 +35,7 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl slog.String("ttl", ttl), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Issue cert failed to complete successfully", args...) return } @@ -58,7 +58,7 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List certs failed to complete successfully", args...) return } @@ -82,7 +82,7 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List serials failed to complete successfully", args...) return } @@ -101,7 +101,7 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin slog.String("serial_id", serialID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View cert failed to complete successfully", args...) return } @@ -120,7 +120,7 @@ func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID stri slog.String("thing_id", thingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Revoke cert failed to complete successfully", args...) return } diff --git a/coap/api/logging.go b/coap/api/logging.go index 1464c5f25..758b26ab3 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -39,7 +39,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa args = append(args, slog.String("channel", destChannel), slog.String("subtopic", msg.GetSubtopic())) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Publish failed to complete successfully", args...) return } @@ -62,7 +62,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Subscribe failed to complete successfully", args...) return } @@ -85,7 +85,7 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Unsubscribe failed to complete successfully", args...) return } diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index fb6c5bce0..145151fc1 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -38,7 +38,7 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create subscription failed to complete successfully", args...) return } @@ -61,7 +61,7 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View subscription failed to complete successfully", args...) return } @@ -85,7 +85,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List subscriptions failed to complete successfully", args...) return } @@ -104,7 +104,7 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove subscription failed to complete successfully", args...) return } @@ -122,7 +122,7 @@ func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{ slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Consume blocking failed to complete successfully", args...) return } diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 9157083db..25c428c45 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -37,7 +37,7 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create group failed to complete successfully", args...) return } @@ -60,7 +60,7 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update group failed to complete successfully", args...) return } @@ -82,7 +82,7 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View group failed to complete successfully", args...) return } @@ -100,7 +100,7 @@ func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id strin slog.String("group_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View group permissions failed to complete successfully", args...) return } @@ -126,7 +126,7 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List groups failed to complete successfully", args...) return } @@ -148,7 +148,7 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Enable group failed to complete successfully", args...) return } @@ -166,7 +166,7 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) slog.String("group_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disable group failed to complete successfully", args...) return } @@ -189,7 +189,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List members failed to complete successfully", args...) return } @@ -208,7 +208,7 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio slog.Any("member_ids", memberIDs), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Assign failed to complete successfully", args...) return } @@ -228,7 +228,7 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat slog.Any("member_ids", memberIDs), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Unassign failed to complete successfully", args...) return } @@ -245,7 +245,7 @@ func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) slog.String("group_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Delete group failed to complete successfully", args...) return } diff --git a/lora/api/logging.go b/lora/api/logging.go index eb5bada83..c0584e1c5 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -39,7 +39,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create thing failed to complete successfully", args...) return } @@ -60,7 +60,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update thing failed to complete successfully", args...) return } @@ -77,7 +77,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er slog.String("thing_id", thingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove thing failed to complete successfully", args...) return } @@ -98,7 +98,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create channel failed to complete successfully", args...) return } @@ -191,7 +191,7 @@ func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Publish failed to complete successfully", args...) return } diff --git a/opcua/api/logging.go b/opcua/api/logging.go index c4a8a492a..1728d52e9 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -39,7 +39,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create thing failed to complete successfully", args...) return } @@ -60,7 +60,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update thing failed to complete successfully", args...) return } @@ -77,7 +77,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e slog.String("thing_id", mgxThing), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove thing failed to complete successfully", args...) return } @@ -98,7 +98,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create channel failed to complete successfully", args...) return } @@ -119,7 +119,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update channel failed to complete successfully", args...) return } @@ -136,7 +136,7 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) slog.String("channel_id", mgxChanID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove channel failed to complete successfully", args...) return } @@ -154,7 +154,7 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin slog.String("thing_id", mgxThingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Connect thing failed to complete successfully", args...) return } @@ -172,7 +172,7 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT slog.String("thing_id", mgxThingID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disconnect thing failed to complete successfully", args...) return } @@ -191,7 +191,7 @@ func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, id slog.String("identifier", identifier), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Browse failed to complete successfully", args...) return } diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 4b2e4feb3..50bdc0f87 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -27,7 +27,7 @@ func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("AuthConnect failed to complete successfully", args...) return } @@ -46,7 +46,7 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay slog.Any("payload", payload), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("AuthPublish failed to complete successfully", args...) return } @@ -64,7 +64,7 @@ func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string slog.Any("topics", topics), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("AuthSubscribe failed to complete successfully", args...) return } @@ -81,7 +81,7 @@ func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Connect failed to complete successfully", args...) return } @@ -98,7 +98,7 @@ func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disconnect failed to complete successfully", args...) return } @@ -118,7 +118,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, topic *string, payload slog.Any("payload", payload), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Publish failed to complete successfully", args...) return } @@ -136,7 +136,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (e slog.Any("topics", topics), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Subscribe failed to complete successfully", args...) return } @@ -154,7 +154,7 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) slog.Any("topics", topics), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Unsubscribe failed to complete successfully", args...) return } diff --git a/provision/api/logging.go b/provision/api/logging.go index 2cbe152cf..9d7a96afd 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -33,7 +33,7 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri slog.String("external_key", externalKey), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Provision failed to complete successfully", args...) return } @@ -51,7 +51,7 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st slog.String("duration", duration), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Cert failed to complete successfully", args...) return } @@ -67,7 +67,7 @@ func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Mapping failed to complete successfully", args...) return } diff --git a/readers/api/logging.go b/readers/api/logging.go index 65a87c2ae..1f55d4d18 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -41,7 +41,7 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Read all failed to complete successfully", args...) return } diff --git a/things/api/logging.go b/things/api/logging.go index f8d20b065..d1ce8c279 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -30,7 +30,7 @@ func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, cli slog.String("duration", time.Since(begin).String()), slog.Any("no_of_things", len(clients)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Create thing failed to complete successfully", args...) return } @@ -46,7 +46,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View client failed to complete successfully", args...) return } @@ -62,7 +62,7 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View client permissions failed to complete successfully", args...) return } @@ -83,7 +83,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List clients failed to complete successfully", args...) return } @@ -104,7 +104,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client failed to complete successfully", args...) return } @@ -143,7 +143,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client secret failed to complete successfully", args...) return } @@ -159,7 +159,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Enable client failed to complete successfully", args...) return } @@ -175,7 +175,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string slog.String("id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disable client failed to complete successfully", args...) return } @@ -196,7 +196,7 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List clients by group failed to complete successfully", args...) return } @@ -216,7 +216,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Identify failed to complete successfully", args...) return } @@ -233,7 +233,7 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth slog.String("channel_id", req.Object), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Authorize failed to complete successfully", args...) return } @@ -251,7 +251,7 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri slog.String("relation", relation), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Share failed to complete successfully", args...) return } @@ -269,7 +269,7 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st slog.String("relation", relation), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Unshare failed to complete successfully", args...) return } @@ -285,7 +285,7 @@ func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) slog.String("thing_id", id), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Delete client failed to complete successfully", args...) return } diff --git a/twins/api/logging.go b/twins/api/logging.go index eb9c41988..f8ecde1ea 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -38,7 +38,7 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Add twin failed to complete successfully", args...) return } @@ -60,7 +60,7 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update twin failed to complete successfully", args...) return } @@ -77,7 +77,7 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) slog.String("twin_id", twinID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View twin failed to complete successfully", args...) return } @@ -99,7 +99,7 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List twins failed to complete successfully", args...) return } @@ -121,7 +121,7 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Save states failed to complete successfully", args...) return } @@ -143,7 +143,7 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List states failed to complete successfully", args...) return } @@ -160,7 +160,7 @@ func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID strin slog.String("twin_id", twinID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Remove twin failed to complete successfully", args...) return } diff --git a/users/api/logging.go b/users/api/logging.go index 3ea97912c..04597b764 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -35,7 +35,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Register client failed to complete successfully", args...) return } @@ -56,7 +56,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d args = append(args, "access_type", t.AccessType) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Issue token failed to complete successfully", args...) return } @@ -77,7 +77,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom args = append(args, "access_type", t.AccessType) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Refresh token failed to complete successfully", args...) return } @@ -95,7 +95,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( slog.Group("user", slog.String("name", c.Name), slog.String("id", id)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View client failed to complete successfully", args...) return } @@ -113,7 +113,7 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("View profile failed to complete successfully", args...) return } @@ -135,7 +135,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List clients failed to complete successfully", args...) return } @@ -153,7 +153,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), slog.Any("metadata", c.Metadata), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client failed to complete successfully", args...) return } @@ -171,7 +171,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, slog.Group("user", slog.String("id", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client tags failed to complete successfully", args...) return } @@ -189,7 +189,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id slog.Group("user", slog.String("id", c.ID), slog.String("identity", identity)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client identity failed to complete successfully", args...) return } @@ -207,7 +207,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client secret failed to complete successfully", args...) return } @@ -224,7 +224,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Generate reset token failed to complete successfully", args...) return } @@ -241,7 +241,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Reset secret failed to complete successfully", args...) return } @@ -258,7 +258,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Send password reset failed to complete successfully", args...) return } @@ -276,7 +276,7 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Update client role failed to complete successfully", args...) return } @@ -294,7 +294,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) slog.String("id", c.ID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Enable client failed to complete successfully", args...) return } @@ -312,7 +312,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string slog.String("id", c.ID), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Disable client failed to complete successfully", args...) return } @@ -336,7 +336,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, ), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("List members failed to complete successfully", args...) return } @@ -352,7 +352,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id str slog.String("duration", time.Since(begin).String()), } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Identify failed to complete successfully", args...) return } diff --git a/ws/api/logging.go b/ws/api/logging.go index 075ee6bf5..49680d2f4 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -37,7 +37,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, su args = append(args, "subtopic", subtopic, "channel", destChannel) } if err != nil { - args = append(args, slog.String("error", err.Error())) + args = append(args, slog.Any("error", err)) lm.logger.Warn("Subscibe failed to complete successfully", args...) return } From 6ed11f6ec35a2234fb3288ebc789bb4f39b9e772 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 16:26:38 +0300 Subject: [PATCH 11/23] resolve comments Signed-off-by: Musilah --- auth/api/logging.go | 5 +-- coap/api/logging.go | 11 +++--- consumers/notifiers/api/logging.go | 2 +- internal/groups/api/logging.go | 4 +-- provision/api/logging.go | 8 ++--- readers/api/logging.go | 7 ++-- things/api/logging.go | 58 +++++++++++++++--------------- twins/api/logging.go | 6 ++-- users/api/logging.go | 24 +++++++------ ws/api/logging.go | 4 +-- 10 files changed, 63 insertions(+), 66 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index 377174465..a4f931ae8 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -381,8 +381,9 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page slog.String("duration", time.Since(begin).String()), slog.Group( "page", - slog.Any("limit", page.Limit), - slog.Any("offset", page.Offset), + slog.Uint64("limit", page.Limit), + slog.Uint64("offset", page.Offset), + slog.Uint64("total", page.Total), ), } if err != nil { diff --git a/coap/api/logging.go b/coap/api/logging.go index 758b26ab3..23114f6c3 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -32,7 +32,6 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("key", key), } destChannel := msg.GetChannel() if msg.GetSubtopic() != "" { @@ -55,11 +54,10 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("key", key), + slog.String("channel_id", chanID), } - destChannel := chanID if subtopic != "" { - args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) + args = append(args, slog.String("subtopic", subtopic)) } if err != nil { args = append(args, slog.Any("error", err)) @@ -78,11 +76,10 @@ func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subto defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("key", key), + slog.String("channel_id", chanID), } - destChannel := chanID if subtopic != "" { - args = append(args, slog.String("subtopic", subtopic), slog.String("channel", destChannel)) + args = append(args, slog.String("subtopic", subtopic)) } if err != nil { args = append(args, slog.Any("error", err)) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 145151fc1..cb31f11b2 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -80,7 +80,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string slog.Group( "page_metadata", slog.String("topic", pm.Topic), - slog.Any("limit", pm.Limit), + slog.Int("limit", pm.Limit), slog.Any("offset", pm.Offset), ), } diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 25c428c45..ff79d45c3 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -121,8 +121,8 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, slog.String("member_id", memberID), slog.Group( "page", - slog.Any("limit", gp.Limit), - slog.Any("offset", gp.Offset)), + slog.Uint64("limit", gp.Limit), + slog.Uint64("offset", gp.Offset)), ), } if err != nil { diff --git a/provision/api/logging.go b/provision/api/logging.go index 9d7a96afd..b4617f8ee 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -28,9 +28,6 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("name", name), - slog.String("external_id", externalID), - slog.String("external_key", externalKey), } if err != nil { args = append(args, slog.Any("error", err)) @@ -48,14 +45,13 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st args := []interface{}{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), - slog.String("duration", duration), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Cert failed to complete successfully", args...) + lm.logger.Warn("Thing certificate failed to create successfully", args...) return } - lm.logger.Info("Cert completed successfully", args...) + lm.logger.Info("Thing certificate completed successfully", args...) }(time.Now()) return lm.svc.Cert(token, thingID, duration) diff --git a/readers/api/logging.go b/readers/api/logging.go index 1f55d4d18..01df750e2 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -31,11 +31,12 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("channel_ID", chanID), + slog.String("channel_id", chanID), slog.Group( "page_metadata", - slog.Any("offset", rpm.Offset), - slog.Any("limit", rpm.Limit), + slog.Uint64("offset", rpm.Offset), + slog.Uint64("limit", rpm.Limit), + slog.Uint64("total", page.Total), slog.String("subtopic", rpm.Subtopic), slog.String("publisher", rpm.Publisher), ), diff --git a/things/api/logging.go b/things/api/logging.go index d1ce8c279..d83f91f3b 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -27,7 +27,8 @@ func LoggingMiddleware(svc things.Service, logger *slog.Logger) things.Service { func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, clients ...mgclients.Client) (cs []mgclients.Client, err error) { defer func(begin time.Time) { args := []interface{}{ - slog.String("duration", time.Since(begin).String()), slog.Any("no_of_things", len(clients)), + slog.String("duration", time.Since(begin).String()), + slog.Any("no_of_things", len(clients)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -47,10 +48,10 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("View client failed to complete successfully", args...) + lm.logger.Warn("View thing failed to complete successfully", args...) return } - lm.logger.Info("View client completed successfully", args...) + lm.logger.Info("View thing completed successfully", args...) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } @@ -63,10 +64,10 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("View client permissions failed to complete successfully", args...) + lm.logger.Warn("View thing permissions failed to complete successfully", args...) return } - lm.logger.Info("View client permissions completed successfully", args...) + lm.logger.Info("View thing permissions completed successfully", args...) }(time.Now()) return lm.svc.ViewClientPerms(ctx, token, id) } @@ -78,16 +79,17 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s slog.String("user_id", reqUserID), slog.Group( "page", - slog.Any("limit", pm.Limit), - slog.Any("offset", pm.Offset), + slog.Uint64("limit", pm.Limit), + slog.Uint64("offset", pm.Offset), + slog.Uint64("total", cp.Total), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List clients failed to complete successfully", args...) + lm.logger.Warn("List things failed to complete successfully", args...) return } - lm.logger.Info("List clients completed successfully", args...) + lm.logger.Info("List things completed successfully", args...) }(time.Now()) return lm.svc.ListClients(ctx, token, reqUserID, pm) } @@ -105,10 +107,10 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client failed to complete successfully", args...) + lm.logger.Warn("Update thing failed to complete successfully", args...) return } - lm.logger.Info("Update client completed successfully", args...) + lm.logger.Info("Update thing completed successfully", args...) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } @@ -125,10 +127,10 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Update client tags failed to complete successfully", args...) + lm.logger.Warn("Update thing tags failed to complete successfully", args...) return } - lm.logger.Info("Update client tags completed successfully", args...) + lm.logger.Info("Update thing tags completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } @@ -144,10 +146,10 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client secret failed to complete successfully", args...) + lm.logger.Warn("Update thing secret failed to complete successfully", args...) return } - lm.logger.Info("Update client secret completed successfully", args...) + lm.logger.Info("Update thing secret completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } @@ -160,10 +162,10 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Enable client failed to complete successfully", args...) + lm.logger.Warn("Enable thing failed to complete successfully", args...) return } - lm.logger.Info("Enable client completed successfully", args...) + lm.logger.Info("Enable thing completed successfully", args...) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } @@ -176,10 +178,10 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disable client failed to complete successfully", args...) + lm.logger.Warn("Disable thing failed to complete successfully", args...) return } - lm.logger.Info("Disable client completed successfully", args...) + lm.logger.Info("Disable thing completed successfully", args...) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } @@ -191,16 +193,17 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan slog.String("channel_id", channelID), slog.Group( "page", - slog.Any("offset", cp.Offset), - slog.Any("limit", cp.Limit), + slog.Uint64("offset", cp.Offset), + slog.Uint64("limit", cp.Limit), + slog.Uint64("total", mp.Total), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List clients by group failed to complete successfully", args...) + lm.logger.Warn("List things by group failed to complete successfully", args...) return } - lm.logger.Info("List clients by group completed successfully", args...) + lm.logger.Info("List things by group completed successfully", args...) }(time.Now()) return lm.svc.ListClientsByGroup(ctx, token, channelID, cp) } @@ -211,16 +214,15 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin slog.String("duration", time.Since(begin).String()), slog.Group( "thing", - slog.String("key", key), slog.String("id", id), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Identify failed to complete successfully", args...) + lm.logger.Warn("Identify thing failed to complete successfully", args...) return } - lm.logger.Info("Identify completed successfully", args...) + lm.logger.Info("Identify thing completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, key) } @@ -286,10 +288,10 @@ func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Delete client failed to complete successfully", args...) + lm.logger.Warn("Delete thing failed to complete successfully", args...) return } - lm.logger.Info("Delete client completed successfully", args...) + lm.logger.Info("Delete thing completed successfully", args...) }(time.Now()) return lm.svc.DeleteClient(ctx, token, id) } diff --git a/twins/api/logging.go b/twins/api/logging.go index f8ecde1ea..b7570df55 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -32,8 +32,8 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi slog.String("duration", time.Since(begin).String()), slog.Group( "twin", - slog.String("id", twin.ID), - slog.String("name", twin.Name), + slog.String("id", tw.ID), + slog.String("name", tw.Name), slog.Group("definition", slog.Any("def_id", def.ID)), ), } @@ -114,7 +114,7 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess args := []interface{}{ slog.String("duration", time.Since(begin).String()), slog.Group( - "payload", + "message", slog.String("subtopic", msg.Subtopic), slog.String("channel", msg.Channel), slog.String("publisher", msg.Publisher), diff --git a/users/api/logging.go b/users/api/logging.go index 04597b764..4979caa6a 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -32,7 +32,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), + slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -92,7 +92,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("name", c.Name), slog.String("id", id)), + slog.Group("user", slog.String("id", id), slog.String("name", c.Name),), } if err != nil { args = append(args, slog.Any("error", err)) @@ -110,7 +110,7 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), + slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -130,8 +130,9 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m slog.String("duration", time.Since(begin).String()), slog.Group( "page", - slog.Any("limit", pm.Limit), - slog.Any("offset", pm.Offset), + slog.Uint64("limit", pm.Limit), + slog.Uint64("offset", pm.Offset), + slog.Uint64("total", cp.Total), ), } if err != nil { @@ -150,7 +151,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("name", c.Name), slog.String("id", c.ID)), slog.Any("metadata", c.Metadata), + slog.Group("user", slog.String("id", c.ID)), slog.String("name", c.Name), slog.Any("metadata", c.Metadata), } if err != nil { args = append(args, slog.Any("error", err)) @@ -168,7 +169,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("tags", fmt.Sprintf("%v", c.Tags))), + slog.Group("user", slog.String("id", c.ID), slog.Any("tags", fmt.Sprintf("%v", c.Tags))), } if err != nil { args = append(args, slog.Any("error", err)) @@ -186,7 +187,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("identity", identity)), + slog.Group("user", slog.String("id", c.ID)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -222,6 +223,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.String("host", host), } if err != nil { args = append(args, slog.Any("error", err)) @@ -327,12 +329,12 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("object_kind", objectKind), - slog.String("object_id", objectID), + slog.Group("object", slog.String("kind", objectKind), slog.String("id", objectID)), slog.Group( "page", slog.Any("limit", cp.Limit), - slog.Any("offset", cp.Offset), + slog.Uint64("offset", cp.Offset), + slog.Uint64("total", mp.Total), ), } if err != nil { diff --git a/ws/api/logging.go b/ws/api/logging.go index 49680d2f4..387798bff 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -29,12 +29,10 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, su defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("thing_key", thingKey), slog.String("channel_id", chanID), } - destChannel := chanID if subtopic != "" { - args = append(args, "subtopic", subtopic, "channel", destChannel) + args = append(args, "subtopic", subtopic) } if err != nil { args = append(args, slog.Any("error", err)) From c5d46c3e4742db6452ce88cba2e294f399cde1b6 Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 18:16:54 +0300 Subject: [PATCH 12/23] add logAction function Signed-off-by: Musilah --- pkg/messaging/handler/logging.go | 128 ++++++------------------------- 1 file changed, 22 insertions(+), 106 deletions(-) diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 50bdc0f87..03d09c08d 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -22,149 +22,65 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("AuthConnect failed to complete successfully", args...) - return - } - lm.logger.Info("AuthConnect completed successfully", args...) - }(time.Now()) - - return lm.svc.AuthConnect(ctx) + return lm.logAction(ctx, "AuthConnect", nil, nil) } // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - slog.String("topic", *topic), - slog.Any("payload", payload), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("AuthPublish failed to complete successfully", args...) - return - } - lm.logger.Info("AuthPublish completed successfully", args...) - }(time.Now()) - - return lm.svc.AuthPublish(ctx, topic, payload) + return lm.logAction(ctx, "AuthPublish", &[]string{*topic}, payload) } // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - slog.Any("topics", topics), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("AuthSubscribe failed to complete successfully", args...) - return - } - lm.logger.Info("AuthSubscribe completed successfully", args...) - }(time.Now()) - - return lm.svc.AuthSubscribe(ctx, topics) + return lm.logAction(ctx,"AuthSubscribe", topics, nil) } // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("Connect failed to complete successfully", args...) - return - } - lm.logger.Info("Connect completed successfully", args...) - }(time.Now()) - - return lm.svc.Connect(ctx) + return lm.logAction(ctx, "Connect", nil, nil) } // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disconnect failed to complete successfully", args...) - return - } - lm.logger.Info("Disconnect completed successfully", args...) - }(time.Now()) - - return lm.svc.Disconnect(ctx) + return lm.logAction(ctx, "Disconnect", nil, nil) } // 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) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - slog.Any("topic", topic), - slog.Any("payload", payload), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("Publish failed to complete successfully", args...) - return - } - lm.logger.Info("Publish completed successfully", args...) - }(time.Now()) - - return lm.svc.Publish(ctx, topic, payload) + return lm.logAction(ctx, "Publish", &[]string{*topic}, payload) } // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { - defer func(begin time.Time) { - args := []interface{}{ - slog.String("duration", time.Since(begin).String()), - slog.Any("topics", topics), - } - if err != nil { - args = append(args, slog.Any("error", err)) - lm.logger.Warn("Subscribe failed to complete successfully", args...) - return - } - lm.logger.Info("Subscribe completed successfully", args...) - }(time.Now()) - - return lm.svc.Subscribe(ctx, topics) + return lm.logAction(ctx, "Subscribe", topics, nil) } // Unsubscribe implements session.Handler. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) (err error) { + return lm.logAction(ctx, "Unsubscribe", topics, nil) +} + +// 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, payload *[]byte) (err error) { defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Any("topics", topics), + } + if topics != nil { + args = append(args, slog.Any("topics", *topics)) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Unsubscribe failed to complete successfully", args...) + lm.logger.Warn(action+"() failed to complete successfully", args...) return } - lm.logger.Info("Unsubscribe completed successfully", args...) + lm.logger.Info("Action completed successfully", args...) }(time.Now()) - 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} + return nil } From 3053cd81abcaf841d61641340772552cb3f1463e Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 18:45:57 +0300 Subject: [PATCH 13/23] fix linter Signed-off-by: Musilah --- bootstrap/api/logging.go | 2 +- pkg/messaging/handler/logging.go | 2 +- readers/api/logging.go | 8 ++++++-- users/api/logging.go | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 27935d7cb..16bdefc1c 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -169,7 +169,6 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa slog.Group( "config", slog.String("external_id", externalID), - slog.String("external_key", externalKey), ), } if err != nil { @@ -260,6 +259,7 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { args := []interface{}{ + slog.String("duration", time.Since(begin).String()), slog.String("channel_id", channelID), slog.String("thing_id", thingID), } diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 03d09c08d..5a6fbbe30 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -32,7 +32,7 @@ func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, pay // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { - return lm.logAction(ctx,"AuthSubscribe", topics, nil) + return lm.logAction(ctx, "AuthSubscribe", topics, nil) } // Connect implements session.Handler. diff --git a/readers/api/logging.go b/readers/api/logging.go index 01df750e2..92a16d559 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -37,10 +37,14 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p slog.Uint64("offset", rpm.Offset), slog.Uint64("limit", rpm.Limit), slog.Uint64("total", page.Total), - slog.String("subtopic", rpm.Subtopic), - slog.String("publisher", rpm.Publisher), ), } + if rpm.Subtopic != "" { + args = append(args, slog.String("subtopic", rpm.Subtopic)) + } + if rpm.Publisher != "" { + args = append(args, slog.String("publisher", rpm.Publisher)) + } if err != nil { args = append(args, slog.Any("error", err)) lm.logger.Warn("Read all failed to complete successfully", args...) diff --git a/users/api/logging.go b/users/api/logging.go index 4979caa6a..0548c3646 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -92,7 +92,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", id), slog.String("name", c.Name),), + slog.Group("user", slog.String("id", id), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -258,6 +258,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.String("host", host), } if err != nil { args = append(args, slog.Any("error", err)) From 39b13b53123f2aebb35b8b5fc759a240cbe922cc Mon Sep 17 00:00:00 2001 From: Musilah Date: Mon, 22 Jan 2024 19:43:18 +0300 Subject: [PATCH 14/23] user getter functions Signed-off-by: Musilah --- things/api/logging.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/things/api/logging.go b/things/api/logging.go index d83f91f3b..e8f71d74d 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -231,8 +231,8 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.String("thing_key", req.Subject), - slog.String("channel_id", req.Object), + slog.String("thing_key", req.GetSubject()), + slog.String("channel_id", req.GetObject()), } if err != nil { args = append(args, slog.Any("error", err)) From 9ec30686164c2ea43045f43554c20587354dab74 Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 01:32:06 +0300 Subject: [PATCH 15/23] fix method logs in users Signed-off-by: Musilah --- users/api/logging.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/users/api/logging.go b/users/api/logging.go index 0548c3646..cd3d72f7a 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -36,10 +36,10 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Register client failed to complete successfully", args...) + lm.logger.Warn("Register user failed to complete successfully", args...) return } - lm.logger.Info("Register client completed successfully", args...) + lm.logger.Info("Register user completed successfully", args...) }(time.Now()) return lm.svc.RegisterClient(ctx, token, client) } @@ -96,10 +96,10 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("View client failed to complete successfully", args...) + lm.logger.Warn("View user failed to complete successfully", args...) return } - lm.logger.Info("View client completed successfully", args...) + lm.logger.Info("View user completed successfully", args...) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } @@ -137,10 +137,10 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List clients failed to complete successfully", args...) + lm.logger.Warn("List users failed to complete successfully", args...) return } - lm.logger.Info("List clients completed successfully", args...) + lm.logger.Info("List users completed successfully", args...) }(time.Now()) return lm.svc.ListClients(ctx, token, pm) } @@ -155,10 +155,10 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client failed to complete successfully", args...) + lm.logger.Warn("Update user failed to complete successfully", args...) return } - lm.logger.Info("Update client completed successfully", args...) + lm.logger.Info("Update user completed successfully", args...) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } @@ -173,10 +173,10 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client tags failed to complete successfully", args...) + lm.logger.Warn("Update user tags failed to complete successfully", args...) return } - lm.logger.Info("Update client tags completed successfully", args...) + lm.logger.Info("Update user tags completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } @@ -209,10 +209,10 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client secret failed to complete successfully", args...) + lm.logger.Warn("Update user secret failed to complete successfully", args...) return } - lm.logger.Info("Update client secret completed successfully", args...) + lm.logger.Info("Update user secret completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } @@ -280,10 +280,10 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update client role failed to complete successfully", args...) + lm.logger.Warn("Update user role failed to complete successfully", args...) return } - lm.logger.Info("Update client role completed successfully", args...) + lm.logger.Info("Update user role completed successfully", args...) }(time.Now()) return lm.svc.UpdateClientRole(ctx, token, client) } @@ -298,10 +298,10 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Enable client failed to complete successfully", args...) + lm.logger.Warn("Enable user failed to complete successfully", args...) return } - lm.logger.Info("Enable client completed successfully", args...) + lm.logger.Info("Enable user completed successfully", args...) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } @@ -316,10 +316,10 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disable client failed to complete successfully", args...) + lm.logger.Warn("Disable user failed to complete successfully", args...) return } - lm.logger.Info("Disable client completed successfully", args...) + lm.logger.Info("Disable user completed successfully", args...) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } From 2da9a6fd6af9115755291a4527e55c96c0ee7b33 Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 13:25:11 +0300 Subject: [PATCH 16/23] resolve new comments Signed-off-by: Musilah --- auth/api/logging.go | 123 ++++++++++++++++++++++++++--- bootstrap/api/logging.go | 26 +++--- certs/api/logging.go | 4 +- coap/api/logging.go | 4 +- consumers/notifiers/api/logging.go | 4 +- consumers/writers/api/logging.go | 10 ++- internal/groups/api/logging.go | 18 ++--- lora/api/logging.go | 12 +-- opcua/api/logging.go | 12 +-- readers/api/logging.go | 2 +- users/api/logging.go | 2 +- 11 files changed, 161 insertions(+), 56 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index a4f931ae8..25d4e2b84 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -29,7 +29,7 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Any("limit", limit), + slog.Int64("limit", int64(limit)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -46,6 +46,13 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("kind", pr.ObjectKind), + slog.String("type", pr.ObjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -62,6 +69,13 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("kind", pr.ObjectKind), + slog.String("type", pr.ObjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -77,7 +91,14 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Any("limit", limit), + slog.Int64("limit", int64(limit)), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -94,6 +115,12 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -110,6 +137,12 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -142,16 +175,21 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "key", + slog.String("id", key.ID), + slog.Any("type", key.Type), + ), } if key.Type != auth.AccessKey && !key.ExpiresAt.IsZero() { args = append(args, slog.Any("expiration_date", key.ExpiresAt)) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Issue failed to complete successfully", args...) + lm.logger.Warn("Issue key failed to complete successfully", args...) return } - lm.logger.Info("Issue completed successfully", args...) + lm.logger.Info("Issue key completed successfully", args...) }(time.Now()) return lm.svc.Issue(ctx, token, key) @@ -165,10 +203,10 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Revoke failed to complete successfully", args...) + lm.logger.Warn("Revoke key failed to complete successfully", args...) return } - lm.logger.Info("Revoke completed successfully", args...) + lm.logger.Info("Revoke key completed successfully", args...) }(time.Now()) return lm.svc.Revoke(ctx, token, id) @@ -198,10 +236,10 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Identify failed to complete successfully", args...) + lm.logger.Warn("Identify key failed to complete successfully", args...) return } - lm.logger.Info("Identify completed successfully", args...) + lm.logger.Info("Identify key completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, token) @@ -211,6 +249,19 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("kind", pr.ObjectKind), + slog.String("type", pr.ObjectType), + ), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -226,6 +277,19 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("kind", pr.ObjectKind), + slog.String("type", pr.ObjectType), + ), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -241,6 +305,19 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "subject", + slog.String("id", prs[0].Subject), + slog.String("kind", prs[0].SubjectKind), + slog.String("type", prs[0].SubjectType), + ), + slog.Group( + "object", + slog.String("id", prs[0].Object), + slog.String("kind", prs[0].ObjectKind), + slog.String("type", prs[0].ObjectType), + ), + slog.String("relation", prs[0].Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -257,6 +334,19 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("kind", pr.ObjectKind), + slog.String("type", pr.ObjectType), + ), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), + slog.String("type", pr.SubjectType), + ), + slog.String("relation", pr.Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -272,6 +362,19 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.Group( + "subject", + slog.String("id", prs[0].Subject), + slog.String("kind", prs[0].SubjectKind), + slog.String("type", prs[0].SubjectType), + ), + slog.Group( + "object", + slog.String("id", prs[0].Object), + slog.String("kind", prs[0].ObjectKind), + slog.String("type", prs[0].ObjectType), + ), + slog.String("relation", prs[0].Relation), } if err != nil { args = append(args, slog.Any("error", err)) @@ -341,7 +444,7 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, slog.String("duration", time.Since(begin).String()), slog.Group( "domain", - slog.String("domain_id", id), + slog.String("id", id), slog.Any("name", d.Name), ), } @@ -361,7 +464,7 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s slog.String("duration", time.Since(begin).String()), slog.Group( "domain", - slog.String("domain_id", id), + slog.String("id", id), slog.Any("status", d.Status), ), } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 16bdefc1c..55ac85846 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -35,10 +35,10 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Add failed to complete successfully", args...) + lm.logger.Warn("Add new Thing config failed to complete successfully", args...) return } - lm.logger.Info("Add completed successfully", args...) + lm.logger.Info("Add new Thing config completed successfully", args...) }(time.Now()) return lm.svc.Add(ctx, token, cfg) @@ -54,10 +54,10 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("View failed to complete successfully", args...) + lm.logger.Warn("View thing config failed to complete successfully", args...) return } - lm.logger.Info("View completed successfully", args...) + lm.logger.Info("View thing config completed successfully", args...) }(time.Now()) return lm.svc.View(ctx, token, id) @@ -73,10 +73,10 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update failed to complete successfully", args...) + lm.logger.Warn("Update config failed to complete successfully", args...) return } - lm.logger.Info("Update completed successfully", args...) + lm.logger.Info("Update config completed successfully", args...) }(time.Now()) return lm.svc.Update(ctx, token, cfg) @@ -134,10 +134,10 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List failed to complete successfully", args...) + lm.logger.Warn("List configs failed to complete successfully", args...) return } - lm.logger.Info("List completed successfully", args...) + lm.logger.Info("List configs completed successfully", args...) }(time.Now()) return lm.svc.List(ctx, token, filter, offset, limit) @@ -153,10 +153,10 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove failed to complete successfully", args...) + lm.logger.Warn("Remove config failed to complete successfully", args...) return } - lm.logger.Info("Remove completed successfully", args...) + lm.logger.Info("Remove config completed successfully", args...) }(time.Now()) return lm.svc.Remove(ctx, token, id) @@ -206,9 +206,9 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b slog.String("duration", time.Since(begin).String()), slog.Group( "channel", - slog.String("channel_id", channel.ID), - slog.String("channel_name", channel.Name), - slog.Any("channel_metadata", channel.Metadata), + slog.String("id", channel.ID), + slog.String("name", channel.Name), + slog.Any("metadata", channel.Metadata), ), } if err != nil { diff --git a/certs/api/logging.go b/certs/api/logging.go index 278d3bef3..c932ba906 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -83,10 +83,10 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List serials failed to complete successfully", args...) + lm.logger.Warn("List certifcates failed to complete successfully", args...) return } - lm.logger.Info("List serials completed successfully", args...) + lm.logger.Info("List certificates completed successfully", args...) }(time.Now()) return lm.svc.ListSerials(ctx, token, thingID, offset, limit) diff --git a/coap/api/logging.go b/coap/api/logging.go index 23114f6c3..97f4da086 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -32,10 +32,10 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), + slog.String("channel_id", msg.GetChannel()), } - destChannel := msg.GetChannel() if msg.GetSubtopic() != "" { - args = append(args, slog.String("channel", destChannel), slog.String("subtopic", msg.GetSubtopic())) + args = append(args, slog.String("subtopic", msg.GetSubtopic())) } if err != nil { args = append(args, slog.Any("error", err)) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index cb31f11b2..392ab5daa 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -78,10 +78,10 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string args := []interface{}{ slog.String("duration", time.Since(begin).String()), slog.Group( - "page_metadata", + "page", slog.String("topic", pm.Topic), slog.Int("limit", pm.Limit), - slog.Any("offset", pm.Offset), + slog.Uint64("offset", uint64(pm.Offset)), ), } if err != nil { diff --git a/consumers/writers/api/logging.go b/consumers/writers/api/logging.go index f5c3f41a3..419ad400c 100644 --- a/consumers/writers/api/logging.go +++ b/consumers/writers/api/logging.go @@ -7,7 +7,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -33,12 +32,15 @@ func LoggingMiddleware(consumer consumers.BlockingConsumer, logger *slog.Logger) // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msgs interface{}) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method consume took %s to complete", time.Since(begin)) + args := []interface{}{ + slog.String("duration", time.Since(begin).String()), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("Consume blocking failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("Consume blocking completed successfully", args...) }(time.Now()) return lm.consumer.ConsumeBlocking(ctx, msgs) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index ff79d45c3..e31fca535 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -31,9 +31,9 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("group_name", g.Name), + slog.String("name", g.Name), slog.String("kind", kind), - slog.String("group_id", g.ID), + slog.String("id", g.ID), ), } if err != nil { @@ -54,8 +54,8 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("group_name", g.Name), - slog.String("group_id", g.ID), + slog.String("name", g.Name), + slog.String("id", g.ID), slog.Any("metadata", g.Metadata), ), } @@ -77,8 +77,8 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("group_id", g.ID), - slog.String("group_name", g.Name), + slog.String("id", g.ID), + slog.String("name", g.Name), ), } if err != nil { @@ -143,8 +143,8 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("group_id", id), - slog.String("group_name", g.Name), + slog.String("id", id), + slog.String("name", g.Name), ), } if err != nil { @@ -183,7 +183,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("group_id", groupID), + slog.String("id", groupID), slog.String("permission", permission), slog.String("member_kind", memberKind), ), diff --git a/lora/api/logging.go b/lora/api/logging.go index c0584e1c5..8f9cac50b 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -34,8 +34,8 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI slog.String("duration", time.Since(begin).String()), slog.Group( "thing", - slog.String("thing_id", thingID), - slog.String("lora_dev_eui", loraDevEUI), + slog.String("id", thingID), + slog.String("dev_eui", loraDevEUI), ), } if err != nil { @@ -55,8 +55,8 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI slog.String("duration", time.Since(begin).String()), slog.Group( "thing", - slog.String("thing_id", thingID), - slog.String("lora_dev_eui", loraDevEUI), + slog.String("id", thingID), + slog.String("dev_eui", loraDevEUI), ), } if err != nil { @@ -93,7 +93,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s slog.String("duration", time.Since(begin).String()), slog.Group( "channel", - slog.String("channel_id", chanID), + slog.String("id", chanID), slog.String("lora_app", loraApp), ), } @@ -114,7 +114,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s slog.String("duration", time.Since(begin).String()), slog.Group( "channel", - slog.String("channel_id", chanID), + slog.String("id", chanID), slog.String("lora_app", loraApp), ), } diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 1728d52e9..91910474b 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -34,7 +34,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode slog.String("duration", time.Since(begin).String()), slog.Group( "thing", - slog.String("thing_id", mgxThing), + slog.String("id", mgxThing), slog.String("opcua_node_id", opcuaNodeID), ), } @@ -55,7 +55,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode slog.String("duration", time.Since(begin).String()), slog.Group( "thing", - slog.String("thing_id", mgxThing), + slog.String("id", mgxThing), slog.String("opcua_node_id", opcuaNodeID), ), } @@ -93,8 +93,8 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer slog.String("duration", time.Since(begin).String()), slog.Group( "channel", - slog.String("channel_id", mgxChan), - slog.String("opcua_server_uri", opcuaServerURI), + slog.String("id", mgxChan), + slog.String("server_uri", opcuaServerURI), ), } if err != nil { @@ -114,8 +114,8 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS slog.String("duration", time.Since(begin).String()), slog.Group( "channel", - slog.String("channel_id", mgxChanID), - slog.String("opcua_server_uri", opcuaServerURI), + slog.String("id", mgxChanID), + slog.String("server_uri", opcuaServerURI), ), } if err != nil { diff --git a/readers/api/logging.go b/readers/api/logging.go index 92a16d559..4b6e910de 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -33,7 +33,7 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), slog.Group( - "page_metadata", + "page", slog.Uint64("offset", rpm.Offset), slog.Uint64("limit", rpm.Limit), slog.Uint64("total", page.Total), diff --git a/users/api/logging.go b/users/api/logging.go index cd3d72f7a..6fd9de637 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -333,7 +333,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, slog.Group("object", slog.String("kind", objectKind), slog.String("id", objectID)), slog.Group( "page", - slog.Any("limit", cp.Limit), + slog.Uint64("limit", cp.Limit), slog.Uint64("offset", cp.Offset), slog.Uint64("total", mp.Total), ), From 86a9b562f5db7af7f8cac0fbfcb7e0f0c6ef82f2 Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 14:59:25 +0300 Subject: [PATCH 17/23] revise permissions Signed-off-by: Musilah --- auth/api/logging.go | 53 +++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index 25d4e2b84..4b991598b 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -48,11 +48,14 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR slog.String("duration", time.Since(begin).String()), slog.Group( "object", - slog.String("id", pr.Object), - slog.String("kind", pr.ObjectKind), slog.String("type", pr.ObjectType), ), - slog.String("relation", pr.Relation), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("type", pr.SubjectType), + ), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -72,10 +75,9 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq slog.Group( "object", slog.String("id", pr.Object), - slog.String("kind", pr.ObjectKind), slog.String("type", pr.ObjectType), ), - slog.String("relation", pr.Relation), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -91,14 +93,16 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []interface{}{ slog.String("duration", time.Since(begin).String()), - slog.Int64("limit", int64(limit)), slog.Group( "subject", - slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), - slog.String("relation", pr.Relation), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("type", pr.ObjectType), + ), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -140,7 +144,6 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe slog.Group( "subject", slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), } @@ -159,6 +162,16 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy args := []interface{}{ slog.String("duration", time.Since(begin).String()), slog.Any("filter_permissions", filterPermissions), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("type", pr.ObjectType), + ), + slog.Group( + "subject", + slog.String("id", pr.Subject), + slog.String("type", pr.SubjectType), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -261,7 +274,7 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), - slog.String("relation", pr.Relation), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -280,16 +293,14 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( slog.Group( "object", slog.String("id", pr.Object), - slog.String("kind", pr.ObjectKind), slog.String("type", pr.ObjectType), ), slog.Group( "subject", slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), - slog.String("relation", pr.Relation), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -308,16 +319,14 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR slog.Group( "subject", slog.String("id", prs[0].Subject), - slog.String("kind", prs[0].SubjectKind), slog.String("type", prs[0].SubjectType), ), slog.Group( "object", slog.String("id", prs[0].Object), - slog.String("kind", prs[0].ObjectKind), slog.String("type", prs[0].ObjectType), ), - slog.String("relation", prs[0].Relation), + slog.String("permission", prs[0].Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -337,16 +346,14 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq slog.Group( "object", slog.String("id", pr.Object), - slog.String("kind", pr.ObjectKind), slog.String("type", pr.ObjectType), ), slog.Group( "subject", slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), - slog.String("relation", pr.Relation), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -365,16 +372,14 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli slog.Group( "subject", slog.String("id", prs[0].Subject), - slog.String("kind", prs[0].SubjectKind), slog.String("type", prs[0].SubjectType), ), slog.Group( "object", slog.String("id", prs[0].Object), - slog.String("kind", prs[0].ObjectKind), slog.String("type", prs[0].ObjectType), ), - slog.String("relation", prs[0].Relation), + slog.String("permission", prs[0].Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -392,7 +397,7 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a slog.String("duration", time.Since(begin).String()), slog.Group( "domain", - slog.String("domain_id", d.ID), + slog.String("id", d.ID), slog.String("name", d.Name), ), } From dbad024e64fd9912286c474150b8fbfbedc1924d Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 16:40:22 +0300 Subject: [PATCH 18/23] remove group kind Signed-off-by: Musilah --- internal/groups/api/logging.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index e31fca535..567771453 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -32,7 +32,6 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string slog.Group( "group", slog.String("name", g.Name), - slog.String("kind", kind), slog.String("id", g.ID), ), } From 0b381e004cd7f0dce9da35530fbcc4ad58dc0177 Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 18:09:08 +0300 Subject: [PATCH 19/23] fix list subjects Signed-off-by: Musilah --- auth/api/logging.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index 4b991598b..ab4b08474 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -121,10 +121,14 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy slog.String("duration", time.Since(begin).String()), slog.Group( "subject", - slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), + slog.Group( + "object", + slog.String("id", pr.Object), + slog.String("type", pr.ObjectType), + ), + slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) From 9796575899c4a2d9585b462c9b1119f1cb7b2460 Mon Sep 17 00:00:00 2001 From: Musilah Date: Tue, 23 Jan 2024 19:07:44 +0300 Subject: [PATCH 20/23] fix log messages Signed-off-by: Musilah --- auth/api/logging.go | 2 -- certs/api/logging.go | 16 ++++++------- coap/api/logging.go | 4 ++-- consumers/notifiers/api/logging.go | 2 +- internal/groups/api/logging.go | 12 +++++----- lora/api/logging.go | 32 +++++++++++++------------- opcua/api/logging.go | 36 +++++++++++++++--------------- pkg/messaging/handler/logging.go | 2 +- provision/api/logging.go | 2 +- 9 files changed, 53 insertions(+), 55 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index ab4b08474..fec43fd28 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -269,13 +269,11 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( slog.Group( "object", slog.String("id", pr.Object), - slog.String("kind", pr.ObjectKind), slog.String("type", pr.ObjectType), ), slog.Group( "subject", slog.String("id", pr.Subject), - slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), slog.String("permission", pr.Permission), diff --git a/certs/api/logging.go b/certs/api/logging.go index c932ba906..a6c68116f 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -36,10 +36,10 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Issue cert failed to complete successfully", args...) + lm.logger.Warn("Issue certificate failed to complete successfully", args...) return } - lm.logger.Info("Issue cert completed successfully", args...) + lm.logger.Info("Issue certificate completed successfully", args...) }(time.Now()) return lm.svc.IssueCert(ctx, token, thingID, ttl) @@ -59,10 +59,10 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List certs failed to complete successfully", args...) + lm.logger.Warn("List certificates failed to complete successfully", args...) return } - lm.logger.Info("List certs completed successfully", args...) + lm.logger.Info("List certificates completed successfully", args...) }(time.Now()) return lm.svc.ListCerts(ctx, token, thingID, offset, limit) @@ -102,10 +102,10 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("View cert failed to complete successfully", args...) + lm.logger.Warn("View certificate failed to complete successfully", args...) return } - lm.logger.Info("View cert completed successfully", args...) + lm.logger.Info("View certificate completed successfully", args...) }(time.Now()) return lm.svc.ViewCert(ctx, token, serialID) @@ -121,10 +121,10 @@ func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID stri } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Revoke cert failed to complete successfully", args...) + lm.logger.Warn("Revoke certificate failed to complete successfully", args...) return } - lm.logger.Info("Revoke cert completed successfully", args...) + lm.logger.Info("Revoke certificate completed successfully", args...) }(time.Now()) return lm.svc.RevokeCert(ctx, token, thingID) diff --git a/coap/api/logging.go b/coap/api/logging.go index 97f4da086..c2a562ddb 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -39,10 +39,10 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Publish failed to complete successfully", args...) + lm.logger.Warn("Publish message failed to complete successfully", args...) return } - lm.logger.Info("Publish completed successfully", args...) + lm.logger.Info("Publish message completed successfully", args...) }(time.Now()) return lm.svc.Publish(ctx, key, msg) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 392ab5daa..337498bdf 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -56,7 +56,7 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic slog.String("duration", time.Since(begin).String()), slog.Group( "subscription", - slog.String("topic", sub.Topic), + slog.String("topic", topic), slog.String("id", sub.ID), ), } diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 567771453..21cc28a3f 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -31,8 +31,8 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("name", g.Name), slog.String("id", g.ID), + slog.String("name", g.Name), ), } if err != nil { @@ -53,8 +53,8 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou slog.String("duration", time.Since(begin).String()), slog.Group( "group", - slog.String("name", g.Name), slog.String("id", g.ID), + slog.String("name", g.Name), slog.Any("metadata", g.Metadata), ), } @@ -208,10 +208,10 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Assign failed to complete successfully", args...) + lm.logger.Warn("Assign member failed to complete successfully", args...) return } - lm.logger.Info("Assign completed successfully", args...) + lm.logger.Info("Assign member completed successfully", args...) }(time.Now()) return lm.svc.Assign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -228,10 +228,10 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Unassign failed to complete successfully", args...) + lm.logger.Warn("Unassign member failed to complete successfully", args...) return } - lm.logger.Info("Unassign completed successfully", args...) + lm.logger.Info("Unassign member completed successfully", args...) }(time.Now()) return lm.svc.Unassign(ctx, token, groupID, relation, memberKind, memberIDs...) diff --git a/lora/api/logging.go b/lora/api/logging.go index 8f9cac50b..821cad3f1 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -40,10 +40,10 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thing failed to complete successfully", args...) + lm.logger.Warn("Create thingID:devEUI route-map failed to complete successfully", args...) return } - lm.logger.Info("Create thing completed successfully", args...) + lm.logger.Info("Create thingID:devEUI route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, thingID, loraDevEUI) @@ -61,10 +61,10 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thing failed to complete successfully", args...) + lm.logger.Warn("Update thingID:devEUI route-map failed to complete successfully", args...) return } - lm.logger.Info("Update thing completed successfully", args...) + lm.logger.Info("Update thingID:devEUI route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, thingID, loraDevEUI) @@ -78,10 +78,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thing failed to complete successfully", args...) + lm.logger.Warn("Remove thingID:devEUI route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove thing completed successfully", args...) + lm.logger.Info("Remove thingID:devEUI route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, thingID) @@ -99,10 +99,10 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channel failed to complete successfully", args...) + lm.logger.Warn("Create channelID:appID route-map failed to complete successfully", args...) return } - lm.logger.Info("Create channel completed successfully", args...) + lm.logger.Info("Create channelID:appID route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, chanID, loraApp) @@ -119,10 +119,10 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s ), } if err != nil { - lm.logger.Warn("Update channel failed to complete successfully", args...) + lm.logger.Warn("Update channelID:appID route-map failed to complete successfully", args...) return } - lm.logger.Info("Update channel completed successfully", args...) + lm.logger.Info("Update channelID:appID route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, chanID, loraApp) @@ -135,10 +135,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e slog.String("channel_id", chanID), } if err != nil { - lm.logger.Warn("Remove channel failed to complete successfully", args...) + lm.logger.Warn("Remove channelID:appID route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove channel completed successfully", args...) + lm.logger.Info("Remove channelID:appID route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, chanID) @@ -153,10 +153,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Connect thing failed to complete successfully", args...) + lm.logger.Warn("Connect thingID:channelID route-map failed to complete successfully", args...) return } - lm.logger.Info("Connect thing completed successfully", args...) + lm.logger.Info("Connect thingID:channelID route-map completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, chanID, thingID) @@ -171,10 +171,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Disconnect thing failed to complete successfully", args...) + lm.logger.Warn("Disconnect thingID:channelID route-map failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thing completed successfully", args...) + lm.logger.Info("Disconnect thingID:channelID route-map completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, chanID, thingID) diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 91910474b..0333093d3 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -35,15 +35,15 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode slog.Group( "thing", slog.String("id", mgxThing), - slog.String("opcua_node_id", opcuaNodeID), + slog.String("node_id", opcuaNodeID), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thing failed to complete successfully", args...) + lm.logger.Warn("Create thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) return } - lm.logger.Info("Create thing completed successfully", args...) + lm.logger.Info("Create thingID:OPC-UA-nodeID route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) @@ -56,15 +56,15 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode slog.Group( "thing", slog.String("id", mgxThing), - slog.String("opcua_node_id", opcuaNodeID), + slog.String("node_id", opcuaNodeID), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thing failed to complete successfully", args...) + lm.logger.Warn("Update thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) return } - lm.logger.Info("Update thing completed successfully", args...) + lm.logger.Info("Update thingID:OPC-UA-nodeID route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, mgxThing, opcuaNodeID) @@ -78,10 +78,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thing failed to complete successfully", args...) + lm.logger.Warn("Remove thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove thing completed successfully", args...) + lm.logger.Info("Remove thingID:OPC-UA-nodeID route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, mgxThing) @@ -99,10 +99,10 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channel failed to complete successfully", args...) + lm.logger.Warn("Create channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) return } - lm.logger.Info("Create channel completed successfully", args...) + lm.logger.Info("Create channelID:OPC-UA-serverURI route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, mgxChan, opcuaServerURI) @@ -120,10 +120,10 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update channel failed to complete successfully", args...) + lm.logger.Warn("Update channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) return } - lm.logger.Info("Update channel completed successfully", args...) + lm.logger.Info("Update channelID:OPC-UA-serverURI route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, mgxChanID, opcuaServerURI) @@ -137,10 +137,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove channel failed to complete successfully", args...) + lm.logger.Warn("Remove channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove channel completed successfully", args...) + lm.logger.Info("Remove channelID:OPC-UA-serverURI route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, mgxChanID) @@ -155,10 +155,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Connect thing failed to complete successfully", args...) + lm.logger.Warn("Connect thingID:channelID route-map failed to complete successfully", args...) return } - lm.logger.Info("Connect thing completed successfully", args...) + lm.logger.Info("Connect thingID:channelID route-map completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, mgxChanID, mgxThingID) @@ -173,10 +173,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disconnect thing failed to complete successfully", args...) + lm.logger.Warn("Disconnect thingID:channelID route-map failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thing completed successfully", args...) + lm.logger.Info("Disconnect thingID:channelID route-map completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, mgxChanID, mgxThingID) diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 5a6fbbe30..98a22a36a 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -79,7 +79,7 @@ func (lm *loggingMiddleware) logAction(ctx context.Context, action string, topic lm.logger.Warn(action+"() failed to complete successfully", args...) return } - lm.logger.Info("Action completed successfully", args...) + lm.logger.Info(action+"() completed successfully", args...) }(time.Now()) return nil diff --git a/provision/api/logging.go b/provision/api/logging.go index b4617f8ee..2d46aeb3f 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -51,7 +51,7 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st lm.logger.Warn("Thing certificate failed to create successfully", args...) return } - lm.logger.Info("Thing certificate completed successfully", args...) + lm.logger.Info("Thing certificate created successfully", args...) }(time.Now()) return lm.svc.Cert(token, thingID, duration) From 486399eb1c47670a976b8b33244619171502e456 Mon Sep 17 00:00:00 2001 From: Musilah Date: Wed, 24 Jan 2024 13:35:24 +0300 Subject: [PATCH 21/23] squash policy request groups Signed-off-by: Musilah --- auth/api/logging.go | 148 ++++++++++++----------------- bootstrap/api/logging.go | 26 ++--- certs/api/logging.go | 10 +- coap/api/logging.go | 6 +- consumers/notifiers/api/logging.go | 10 +- consumers/writers/api/logging.go | 2 +- internal/groups/api/logging.go | 22 ++--- lora/api/logging.go | 18 ++-- opcua/api/logging.go | 18 ++-- pkg/messaging/handler/logging.go | 2 +- provision/api/logging.go | 6 +- readers/api/logging.go | 2 +- things/api/logging.go | 30 +++--- twins/api/logging.go | 14 +-- users/api/logging.go | 36 +++---- ws/api/logging.go | 2 +- 16 files changed, 164 insertions(+), 188 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index fec43fd28..871485f3e 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -27,7 +27,7 @@ func LoggingMiddleware(svc auth.Service, logger *slog.Logger) auth.Service { func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Int64("limit", int64(limit)), } @@ -44,16 +44,13 @@ func (lm *loggingMiddleware) ListObjects(ctx context.Context, pr auth.PolicyReq, func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "object", - slog.String("type", pr.ObjectType), - ), - slog.Group( - "subject", - slog.String("id", pr.Subject), - slog.String("type", pr.SubjectType), + "policy_request", + slog.String("object_type", pr.ObjectType), + slog.String("subject_id", pr.Subject), + slog.String("subject_type", pr.SubjectType), ), slog.String("permission", pr.Permission), } @@ -70,7 +67,7 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "object", @@ -91,16 +88,13 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq, nextPageToken string, limit int32) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "subject", - slog.String("type", pr.SubjectType), - ), - slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), + "policy_request", + slog.String("subject_type", pr.SubjectType), + slog.String("object_id", pr.Object), + slog.String("object_type", pr.ObjectType), ), slog.String("permission", pr.Permission), } @@ -117,16 +111,13 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.PolicyReq) (p auth.PolicyPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "subject", - slog.String("type", pr.SubjectType), - ), - slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), + "policy_request", + slog.String("sybject_type", pr.SubjectType), + slog.String("object_id", pr.Object), + slog.String("object_type", pr.ObjectType), ), slog.String("permission", pr.Permission), } @@ -143,7 +134,7 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyReq) (count int, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "subject", @@ -163,18 +154,15 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.PolicyReq, filterPermissions []string) (p auth.Permissions, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Any("filter_permissions", filterPermissions), slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), - ), - slog.Group( - "subject", - slog.String("id", pr.Subject), - slog.String("type", pr.SubjectType), + "policy_request", + slog.String("object_id", pr.Object), + slog.String("object_type", pr.ObjectType), + slog.String("subject_id", pr.Subject), + slog.String("subject_type", pr.SubjectType), ), } if err != nil { @@ -190,7 +178,7 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.Key) (tkn auth.Token, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "key", @@ -214,7 +202,7 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -231,7 +219,7 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) (key auth.Key, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -248,7 +236,7 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id auth.Key, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { @@ -264,7 +252,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "object", @@ -290,17 +278,14 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), - ), - slog.Group( - "subject", - slog.String("id", pr.Subject), - slog.String("type", pr.SubjectType), + "policy_request", + slog.String("object_id", pr.Object), + slog.String("object_type", pr.ObjectType), + slog.String("subject_id", pr.Subject), + slog.String("subject_type", pr.SubjectType), ), slog.String("permission", pr.Permission), } @@ -316,17 +301,14 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "subject", - slog.String("id", prs[0].Subject), - slog.String("type", prs[0].SubjectType), - ), - slog.Group( - "object", - slog.String("id", prs[0].Object), - slog.String("type", prs[0].ObjectType), + "policy_request", + slog.String("subject_id", prs[0].Subject), + slog.String("subject_type", prs[0].SubjectType), + slog.String("object_id", prs[0].Object), + slog.String("object_type", prs[0].ObjectType), ), slog.String("permission", prs[0].Permission), } @@ -343,17 +325,14 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), - ), - slog.Group( - "subject", - slog.String("id", pr.Subject), - slog.String("type", pr.SubjectType), + "policy_request", + slog.String("object_id", pr.Object), + slog.String("object_type", pr.ObjectType), + slog.String("subject_id", pr.Subject), + slog.String("subject_type", pr.SubjectType), ), slog.String("permission", pr.Permission), } @@ -369,17 +348,14 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "subject", - slog.String("id", prs[0].Subject), - slog.String("type", prs[0].SubjectType), - ), - slog.Group( - "object", - slog.String("id", prs[0].Object), - slog.String("type", prs[0].ObjectType), + "policy_request", + slog.String("subject_id", prs[0].Subject), + slog.String("subject_type", prs[0].SubjectType), + slog.String("object_id", prs[0].Object), + slog.String("object_type", prs[0].ObjectType), ), slog.String("permission", prs[0].Permission), } @@ -395,7 +371,7 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d auth.Domain) (do auth.Domain, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "domain", @@ -415,7 +391,7 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id string) (do auth.Domain, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("domain_id", id), } @@ -431,7 +407,7 @@ func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id strin func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, token, id string) (permissions auth.Permissions, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("domain_id", id), } @@ -447,7 +423,7 @@ func (lm *loggingMiddleware) RetrieveDomainPermissions(ctx context.Context, toke func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "domain", @@ -467,7 +443,7 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "domain", @@ -487,7 +463,7 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "page", @@ -508,7 +484,7 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), slog.String("relation", relation), @@ -526,7 +502,7 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), slog.String("relation", relation), @@ -544,7 +520,7 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (do auth.DomainsPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("user_id", userID), } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 55ac85846..17161c9f0 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -29,7 +29,7 @@ func LoggingMiddleware(svc bootstrap.Service, logger *slog.Logger) bootstrap.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstrap.Config) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", saved.ThingID), } @@ -48,7 +48,7 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra // If the request fails, it logs the error. func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved bootstrap.Config, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", id), } @@ -67,7 +67,7 @@ func (lm *loggingMiddleware) View(ctx context.Context, token, id string) (saved // If the request fails, it logs the error. func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg bootstrap.Config) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", cfg.ThingID), } @@ -86,7 +86,7 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", cfg.ThingID), } @@ -105,7 +105,7 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id string, connections []string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -124,7 +124,7 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st // If the request fails, it logs the error. func (lm *loggingMiddleware) List(ctx context.Context, token string, filter bootstrap.Filter, offset, limit uint64) (res bootstrap.ConfigsPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "filter", @@ -147,7 +147,7 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot // If the request fails, it logs the error. func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -164,7 +164,7 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (cfg bootstrap.Config, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "config", @@ -184,7 +184,7 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, state bootstrap.State) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), slog.Any("state", state), @@ -202,7 +202,7 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel bootstrap.Channel) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "channel", @@ -224,7 +224,7 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("config_id", id), } @@ -241,7 +241,7 @@ func (lm *loggingMiddleware) RemoveConfigHandler(ctx context.Context, id string) func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -258,7 +258,7 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string func (lm *loggingMiddleware) DisconnectThingHandler(ctx context.Context, channelID, thingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", channelID), slog.String("thing_id", thingID), diff --git a/certs/api/logging.go b/certs/api/logging.go index a6c68116f..c649809ee 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -29,7 +29,7 @@ func LoggingMiddleware(svc certs.Service, logger *slog.Logger) certs.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl string) (c certs.Cert, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), slog.String("ttl", ttl), @@ -48,7 +48,7 @@ func (lm *loggingMiddleware) IssueCert(ctx context.Context, token, thingID, ttl // ListCerts logs the list_certs request. It logs the thing ID and the time it took to complete the request. func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), slog.Group( @@ -72,7 +72,7 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (cp certs.Page, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), slog.Group( @@ -96,7 +96,7 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID string) (c certs.Cert, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("serial_id", serialID), } @@ -115,7 +115,7 @@ func (lm *loggingMiddleware) ViewCert(ctx context.Context, token, serialID strin // If the request fails, it logs the error. func (lm *loggingMiddleware) RevokeCert(ctx context.Context, token, thingID string) (c certs.Revoke, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), } diff --git a/coap/api/logging.go b/coap/api/logging.go index c2a562ddb..081811080 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc coap.Service, logger *slog.Logger) coap.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messaging.Message) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", msg.GetChannel()), } @@ -52,7 +52,7 @@ func (lm *loggingMiddleware) Publish(ctx context.Context, key string, msg *messa // If the request fails, it logs the error. func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopic string, c coap.Client) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), } @@ -74,7 +74,7 @@ func (lm *loggingMiddleware) Subscribe(ctx context.Context, key, chanID, subtopi // If the request fails, it logs the error. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), } diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 337498bdf..9cbc5b001 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -29,7 +29,7 @@ func LoggingMiddleware(svc notifiers.Service, logger *slog.Logger) notifiers.Ser // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token string, sub notifiers.Subscription) (id string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "subscription", @@ -52,7 +52,7 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic string) (sub notifiers.Subscription, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "subscription", @@ -75,7 +75,7 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic // If the request fails, it logs the error. func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string, pm notifiers.PageMetadata) (res notifiers.Page, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "page", @@ -99,7 +99,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string // If the request fails, it logs the error. func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -118,7 +118,7 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{}) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { diff --git a/consumers/writers/api/logging.go b/consumers/writers/api/logging.go index 419ad400c..bba75a595 100644 --- a/consumers/writers/api/logging.go +++ b/consumers/writers/api/logging.go @@ -32,7 +32,7 @@ func LoggingMiddleware(consumer consumers.BlockingConsumer, logger *slog.Logger) // If the request fails, it logs the error. func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msgs interface{}) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 21cc28a3f..0bfbc2432 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -27,7 +27,7 @@ func LoggingMiddleware(svc groups.Service, logger *slog.Logger) groups.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -49,7 +49,7 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, group groups.Group) (g groups.Group, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -72,7 +72,7 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -94,7 +94,7 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("group_id", id), } @@ -112,7 +112,7 @@ func (lm *loggingMiddleware) ViewGroupPerms(ctx context.Context, token, id strin // If the request fails, it logs the error. func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, memberID string, gp groups.Page) (cg groups.Page, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -138,7 +138,7 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -160,7 +160,7 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) (g groups.Group, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("group_id", id), } @@ -178,7 +178,7 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (mp groups.MembersPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "group", @@ -199,7 +199,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("group_id", groupID), slog.String("relation", relation), @@ -219,7 +219,7 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relation, memberKind string, memberIDs ...string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("group_id", groupID), slog.String("relation", relation), @@ -239,7 +239,7 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat func (lm *loggingMiddleware) DeleteGroup(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("group_id", id), } diff --git a/lora/api/logging.go b/lora/api/logging.go index 821cad3f1..17a023197 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc lora.Service, logger *slog.Logger) lora.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "thing", @@ -51,7 +51,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "thing", @@ -72,7 +72,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), } @@ -89,7 +89,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "channel", @@ -110,7 +110,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "channel", @@ -130,7 +130,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), } @@ -146,7 +146,7 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), slog.String("thing_id", thingID), @@ -164,7 +164,7 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), slog.String("thing_id", thingID), @@ -182,7 +182,7 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "message", diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 0333093d3..5b653cee9 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc opcua.Service, logger *slog.Logger) opcua.Service { func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "thing", @@ -51,7 +51,7 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNodeID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "thing", @@ -72,7 +72,7 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", mgxThing), } @@ -89,7 +89,7 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaServerURI string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "channel", @@ -110,7 +110,7 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaServerURI string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "channel", @@ -131,7 +131,7 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", mgxChanID), } @@ -148,7 +148,7 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", mgxChanID), slog.String("thing_id", mgxThingID), @@ -166,7 +166,7 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxThingID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", mgxChanID), slog.String("thing_id", mgxThingID), @@ -184,7 +184,7 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, identifier string) (nodes []opcua.BrowsedNode, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("server_uri", serverURI), slog.String("namespace", namespace), diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index 98a22a36a..d1dda91fa 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -68,7 +68,7 @@ func LoggingMiddleware(svc session.Handler, logger *slog.Logger) session.Handler func (lm *loggingMiddleware) logAction(ctx context.Context, action string, topics *[]string, payload *[]byte) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if topics != nil { diff --git a/provision/api/logging.go b/provision/api/logging.go index 2d46aeb3f..038f3688b 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -26,7 +26,7 @@ func NewLoggingMiddleware(svc provision.Service, logger *slog.Logger) provision. func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey string) (res provision.Result, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { @@ -42,7 +42,7 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), } @@ -59,7 +59,7 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st func (lm *loggingMiddleware) Mapping(token string) (res map[string]interface{}, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { diff --git a/readers/api/logging.go b/readers/api/logging.go index 4b6e910de..c7ea18045 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -29,7 +29,7 @@ func LoggingMiddleware(svc readers.MessageRepository, logger *slog.Logger) reade func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (page readers.MessagesPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), slog.Group( diff --git a/things/api/logging.go b/things/api/logging.go index e8f71d74d..0c3992054 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -26,7 +26,7 @@ func LoggingMiddleware(svc things.Service, logger *slog.Logger) things.Service { func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, clients ...mgclients.Client) (cs []mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Any("no_of_things", len(clients)), } @@ -42,7 +42,7 @@ func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, cli func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -58,7 +58,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id string) (p []string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -74,7 +74,7 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("user_id", reqUserID), slog.Group( @@ -96,7 +96,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "client", @@ -117,7 +117,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "client", @@ -137,7 +137,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "things", @@ -156,7 +156,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -172,7 +172,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", id), } @@ -188,7 +188,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, channelID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", channelID), slog.Group( @@ -210,7 +210,7 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "thing", @@ -229,7 +229,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (id string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_key", req.GetSubject()), slog.String("channel_id", req.GetObject()), @@ -246,7 +246,7 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", id), slog.Any("user_ids", userids), @@ -264,7 +264,7 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation string, userids ...string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", id), slog.Any("user_ids", userids), @@ -282,7 +282,7 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st func (lm *loggingMiddleware) DeleteClient(ctx context.Context, token, id string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", id), } diff --git a/twins/api/logging.go b/twins/api/logging.go index b7570df55..6867eda78 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -28,7 +28,7 @@ func LoggingMiddleware(svc twins.Service, logger *slog.Logger) twins.Service { func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (tw twins.Twin, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "twin", @@ -50,7 +50,7 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin twins.Twin, def twins.Definition) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "twin", @@ -72,7 +72,7 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) (tw twins.Twin, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), } @@ -89,7 +89,7 @@ func (lm *loggingMiddleware) ViewTwin(ctx context.Context, token, twinID string) func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset, limit uint64, name string, metadata twins.Metadata) (page twins.Page, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "page", @@ -111,7 +111,7 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Message) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "message", @@ -133,7 +133,7 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offset, limit uint64, twinID string) (page twins.StatesPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), slog.Group( @@ -155,7 +155,7 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse func (lm *loggingMiddleware) RemoveTwin(ctx context.Context, token, twinID string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), } diff --git a/users/api/logging.go b/users/api/logging.go index 6fd9de637..b734f0279 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -30,7 +30,7 @@ func LoggingMiddleware(svc users.Service, logger *slog.Logger) users.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } @@ -48,7 +48,7 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("domain_id", domainID), } @@ -69,7 +69,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d // If the request fails, it logs the error. func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, domainID string) (t *magistrala.Token, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("domain_id", domainID), } @@ -90,7 +90,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", id), slog.String("name", c.Name)), } @@ -108,7 +108,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } @@ -126,7 +126,7 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mgclients.Page) (cp mgclients.ClientsPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( "page", @@ -149,7 +149,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID)), slog.String("name", c.Name), slog.Any("metadata", c.Metadata), } @@ -167,7 +167,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.Any("tags", fmt.Sprintf("%v", c.Tags))), } @@ -185,7 +185,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID)), } @@ -203,7 +203,7 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), } @@ -221,7 +221,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("host", host), } @@ -239,7 +239,7 @@ func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { @@ -256,7 +256,7 @@ func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret stri // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("host", host), } @@ -274,7 +274,7 @@ func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, client mgclients.Client) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), } @@ -292,7 +292,7 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", c.ID), } @@ -310,7 +310,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mgclients.Client, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("id", c.ID), } @@ -328,7 +328,7 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mgclients.Page) (mp mgclients.MembersPage, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group("object", slog.String("kind", objectKind), slog.String("id", objectID)), slog.Group( @@ -351,7 +351,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, // Identify logs the identify request. It logs the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), } if err != nil { diff --git a/ws/api/logging.go b/ws/api/logging.go index 387798bff..7ecce696f 100644 --- a/ws/api/logging.go +++ b/ws/api/logging.go @@ -27,7 +27,7 @@ func LoggingMiddleware(svc ws.Service, logger *slog.Logger) ws.Service { // If the request fails, it logs the error. func (lm *loggingMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) (err error) { defer func(begin time.Time) { - args := []interface{}{ + args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), } From 5381de86200c8e31b93501012c2d5770b8552656 Mon Sep 17 00:00:00 2001 From: Musilah Date: Wed, 24 Jan 2024 17:32:07 +0300 Subject: [PATCH 22/23] fix pages and log messages Signed-off-by: Musilah --- bootstrap/api/logging.go | 4 +-- certs/api/logging.go | 6 ++-- consumers/notifiers/api/logging.go | 1 + internal/groups/api/logging.go | 31 +++++++++++--------- lora/api/logging.go | 40 +++++++++++++------------- opcua/api/logging.go | 46 +++++++++++++----------------- pkg/messaging/handler/logging.go | 18 ++++++------ provision/api/logging.go | 2 ++ things/api/logging.go | 43 +++++++++++++++++----------- twins/api/logging.go | 12 ++++---- users/api/logging.go | 24 +++++++++++----- 11 files changed, 125 insertions(+), 102 deletions(-) diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 17161c9f0..0d43d8fbb 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -107,7 +107,7 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("thing_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -243,7 +243,7 @@ func (lm *loggingMiddleware) RemoveChannelHandler(ctx context.Context, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("channel_id", id), } if err != nil { args = append(args, slog.Any("error", err)) diff --git a/certs/api/logging.go b/certs/api/logging.go index c649809ee..d7f892540 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -55,6 +55,7 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin "page", slog.Uint64("offset", cp.Offset), slog.Uint64("limit", cp.Limit), + slog.Uint64("total", cp.Total), ), } if err != nil { @@ -79,14 +80,15 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str "page", slog.Uint64("offset", cp.Offset), slog.Uint64("limit", cp.Limit), + slog.Uint64("total", cp.Total), ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("List certifcates failed to complete successfully", args...) + lm.logger.Warn("List certifcates serials failed to complete successfully", args...) return } - lm.logger.Info("List certificates completed successfully", args...) + lm.logger.Info("List certificates serials completed successfully", args...) }(time.Now()) return lm.svc.ListSerials(ctx, token, thingID, offset, limit) diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 9cbc5b001..91b2c4562 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -82,6 +82,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string slog.String("topic", pm.Topic), slog.Int("limit", pm.Limit), slog.Uint64("offset", uint64(pm.Offset)), + slog.Uint64("total", uint64(res.Total)), ), } if err != nil { diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index 0bfbc2432..f57e4e1cd 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -115,13 +115,15 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "group", - slog.String("member_kind", memberKind), - slog.String("member_id", memberID), - slog.Group( - "page", - slog.Uint64("limit", gp.Limit), - slog.Uint64("offset", gp.Offset)), + "member", + slog.String("id", memberID), + slog.String("kind", memberKind), + ), + slog.Group( + "page", + slog.Uint64("limit", gp.Limit), + slog.Uint64("offset", gp.Offset), + slog.Uint64("total", cg.Total), ), } if err != nil { @@ -162,7 +164,11 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("group_id", id), + slog.Group( + "group", + slog.String("id", id), + slog.String("name", g.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -180,12 +186,9 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, groupID, pe defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", - slog.String("id", groupID), - slog.String("permission", permission), - slog.String("member_kind", memberKind), - ), + slog.String("group_id", groupID), + slog.String("permission", permission), + slog.String("member_kind", memberKind), } if err != nil { args = append(args, slog.Any("error", err)) diff --git a/lora/api/logging.go b/lora/api/logging.go index 17a023197..dae317330 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -35,15 +35,15 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI slog.Group( "thing", slog.String("id", thingID), - slog.String("dev_eui", loraDevEUI), ), + slog.String("dev_eui", loraDevEUI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thingID:devEUI route-map failed to complete successfully", args...) + lm.logger.Warn("Create thing %s and lora-dev-eui %s failed to complete successfully", args...) return } - lm.logger.Info("Create thingID:devEUI route-map completed successfully", args...) + lm.logger.Info("Create thing %s and lora-dev-eui %s completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, thingID, loraDevEUI) @@ -56,15 +56,15 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI slog.Group( "thing", slog.String("id", thingID), - slog.String("dev_eui", loraDevEUI), ), + slog.String("dev_eui", loraDevEUI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thingID:devEUI route-map failed to complete successfully", args...) + lm.logger.Warn("Update thing %s and lora-dev-eui %s failed to complete successfully", args...) return } - lm.logger.Info("Update thingID:devEUI route-map completed successfully", args...) + lm.logger.Info("Update thing %s and lora-dev-eui %s completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, thingID, loraDevEUI) @@ -78,10 +78,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thingID:devEUI route-map failed to complete successfully", args...) + lm.logger.Warn("Remove thing failed to complete successfully", args...) return } - lm.logger.Info("Remove thingID:devEUI route-map completed successfully", args...) + lm.logger.Info("Remove thing completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, thingID) @@ -94,15 +94,15 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s slog.Group( "channel", slog.String("id", chanID), - slog.String("lora_app", loraApp), ), + slog.String("lora_app", loraApp), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channelID:appID route-map failed to complete successfully", args...) + lm.logger.Warn("Create channel %s and lora-app %s failed to complete successfully", args...) return } - lm.logger.Info("Create channelID:appID route-map completed successfully", args...) + lm.logger.Info("Create channel %s and lora-app %s completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, chanID, loraApp) @@ -115,14 +115,14 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s slog.Group( "channel", slog.String("id", chanID), - slog.String("lora_app", loraApp), ), + slog.String("lora_app", loraApp), } if err != nil { - lm.logger.Warn("Update channelID:appID route-map failed to complete successfully", args...) + lm.logger.Warn("Update channel %s and lora-app %s failed to complete successfully", args...) return } - lm.logger.Info("Update channelID:appID route-map completed successfully", args...) + lm.logger.Info("Update channel %s and lora-app %s route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, chanID, loraApp) @@ -135,10 +135,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e slog.String("channel_id", chanID), } if err != nil { - lm.logger.Warn("Remove channelID:appID route-map failed to complete successfully", args...) + lm.logger.Warn("Remove channel failed to complete successfully", args...) return } - lm.logger.Info("Remove channelID:appID route-map completed successfully", args...) + lm.logger.Info("Remove channel completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, chanID) @@ -153,10 +153,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Connect thingID:channelID route-map failed to complete successfully", args...) + lm.logger.Warn("Connect thing for channel %s and thing %s failed to complete successfully", args...) return } - lm.logger.Info("Connect thingID:channelID route-map completed successfully", args...) + lm.logger.Info("Connect thing for channel %s and thing %s completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, chanID, thingID) @@ -171,10 +171,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Disconnect thingID:channelID route-map failed to complete successfully", args...) + lm.logger.Warn("Disconnect thing for channel %s and thing %s failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thingID:channelID route-map completed successfully", args...) + lm.logger.Info("Disconnect thing for channel %s and thing %s completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, chanID, thingID) diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 5b653cee9..1b5dd44c7 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -32,18 +32,15 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", mgxThing), - slog.String("node_id", opcuaNodeID), - ), + slog.String("thing_id", mgxThing), + slog.String("node_id", opcuaNodeID), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) + lm.logger.Warn("Create thing %s with nodeID %s failed to complete successfully", args...) return } - lm.logger.Info("Create thingID:OPC-UA-nodeID route-map completed successfully", args...) + lm.logger.Info("Create thing %s with nodeID %s completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) @@ -53,18 +50,15 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", mgxThing), - slog.String("node_id", opcuaNodeID), - ), + slog.String("thing_id", mgxThing), + slog.String("node_id", opcuaNodeID), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) + lm.logger.Warn("Update thing %s with nodeID %s failed to complete successfully", args...) return } - lm.logger.Info("Update thingID:OPC-UA-nodeID route-map completed successfully", args...) + lm.logger.Info("Update thing %s with nodeID %s completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, mgxThing, opcuaNodeID) @@ -78,10 +72,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thingID:OPC-UA-nodeID route-map failed to complete successfully", args...) + lm.logger.Warn("Remove thing failed to complete successfully", args...) return } - lm.logger.Info("Remove thingID:OPC-UA-nodeID route-map completed successfully", args...) + lm.logger.Info("Remove thing completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, mgxThing) @@ -99,10 +93,10 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) + lm.logger.Warn("Create channel %s with ServerURI %s failed to complete successfully", args...) return } - lm.logger.Info("Create channelID:OPC-UA-serverURI route-map completed successfully", args...) + lm.logger.Info("Create channel %s with ServerURI %s completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, mgxChan, opcuaServerURI) @@ -120,10 +114,10 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) + lm.logger.Warn("Update channel %s with ServerURI %s failed to complete successfully", args...) return } - lm.logger.Info("Update channelID:OPC-UA-serverURI route-map completed successfully", args...) + lm.logger.Info("Update channel %s with ServerURI %s completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, mgxChanID, opcuaServerURI) @@ -137,10 +131,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove channelID:OPC-UA-serverURI route-map failed to complete successfully", args...) + lm.logger.Warn("Remove channel %s failed to complete successfully", args...) return } - lm.logger.Info("Remove channelID:OPC-UA-serverURI route-map completed successfully", args...) + lm.logger.Info("Remove channel %s completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, mgxChanID) @@ -155,10 +149,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Connect thingID:channelID route-map failed to complete successfully", args...) + lm.logger.Warn("Connect thing for channel %s and thing %s failed to complete successfully", args...) return } - lm.logger.Info("Connect thingID:channelID route-map completed successfully", args...) + lm.logger.Info("Connect thing for channel %s and thing %s completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, mgxChanID, mgxThingID) @@ -173,10 +167,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disconnect thingID:channelID route-map failed to complete successfully", args...) + lm.logger.Warn("Disconnect thing for channel %s and thing %s failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thingID:channelID route-map completed successfully", args...) + lm.logger.Info("Disconnect thing for channel %s and thing %s completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, mgxChanID, mgxThingID) diff --git a/pkg/messaging/handler/logging.go b/pkg/messaging/handler/logging.go index d1dda91fa..6b92ca67b 100644 --- a/pkg/messaging/handler/logging.go +++ b/pkg/messaging/handler/logging.go @@ -22,43 +22,43 @@ type loggingMiddleware struct { // AuthConnect implements session.Handler. func (lm *loggingMiddleware) AuthConnect(ctx context.Context) (err error) { - return lm.logAction(ctx, "AuthConnect", nil, nil) + return lm.logAction(ctx, "AuthConnect", nil) } // AuthPublish implements session.Handler. func (lm *loggingMiddleware) AuthPublish(ctx context.Context, topic *string, payload *[]byte) (err error) { - return lm.logAction(ctx, "AuthPublish", &[]string{*topic}, payload) + return lm.logAction(ctx, "AuthPublish", &[]string{*topic}) } // AuthSubscribe implements session.Handler. func (lm *loggingMiddleware) AuthSubscribe(ctx context.Context, topics *[]string) (err error) { - return lm.logAction(ctx, "AuthSubscribe", topics, nil) + return lm.logAction(ctx, "AuthSubscribe", topics) } // Connect implements session.Handler. func (lm *loggingMiddleware) Connect(ctx context.Context) (err error) { - return lm.logAction(ctx, "Connect", nil, nil) + return lm.logAction(ctx, "Connect", nil) } // Disconnect implements session.Handler. func (lm *loggingMiddleware) Disconnect(ctx context.Context) (err error) { - return lm.logAction(ctx, "Disconnect", nil, nil) + return lm.logAction(ctx, "Disconnect", nil) } // 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}, payload) + return lm.logAction(ctx, "Publish", &[]string{*topic}) } // Subscribe implements session.Handler. func (lm *loggingMiddleware) Subscribe(ctx context.Context, topics *[]string) (err error) { - return lm.logAction(ctx, "Subscribe", topics, nil) + return lm.logAction(ctx, "Subscribe", topics) } // Unsubscribe implements session.Handler. func (lm *loggingMiddleware) Unsubscribe(ctx context.Context, topics *[]string) (err error) { - return lm.logAction(ctx, "Unsubscribe", topics, nil) + return lm.logAction(ctx, "Unsubscribe", topics) } // LoggingMiddleware adds logging facilities to the adapter. @@ -66,7 +66,7 @@ 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, payload *[]byte) (err error) { +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()), diff --git a/provision/api/logging.go b/provision/api/logging.go index 038f3688b..f6f6a9f63 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -28,6 +28,7 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), + slog.String("external_id", externalID), } if err != nil { args = append(args, slog.Any("error", err)) @@ -45,6 +46,7 @@ func (lm *loggingMiddleware) Cert(token, thingID, duration string) (cert, key st args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), + slog.String("ttl", duration), } if err != nil { args = append(args, slog.Any("error", err)) diff --git a/things/api/logging.go b/things/api/logging.go index 0c3992054..f78b46e6c 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -44,7 +44,7 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.Group("thing", slog.String("id", c.ID), slog.String("name", c.Name)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -60,7 +60,7 @@ func (lm *loggingMiddleware) ViewClientPerms(ctx context.Context, token, id stri defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("thing_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -99,7 +99,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "client", + "thing", slog.String("id", client.ID), slog.String("name", client.Name), slog.Any("metadata", client.Metadata), @@ -120,7 +120,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "client", + "thing", slog.String("id", client.ID), slog.Any("tags", client.Tags), ), @@ -140,8 +140,9 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS args := []any{ slog.String("duration", time.Since(begin).String()), slog.Group( - "things", + "thing", slog.String("id", c.ID), + slog.String("name", c.Name), ), } if err != nil { @@ -158,7 +159,11 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.Group( + "thing", + slog.String("id", id), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -174,7 +179,11 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.Group( + "thing", + slog.String("thing_id", id), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -212,10 +221,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, key string) (id strin defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", id), - ), + slog.String("thing_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -231,8 +237,11 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("thing_key", req.GetSubject()), - slog.String("channel_id", req.GetObject()), + slog.String("object", req.GetObject()), + slog.String("object_type", req.GetObjectType()), + slog.String("subject", req.GetSubject()), + slog.String("subject_type", req.GetSubjectType()), + slog.String("permission", req.GetPermission()), } if err != nil { args = append(args, slog.Any("error", err)) @@ -254,10 +263,10 @@ func (lm *loggingMiddleware) Share(ctx context.Context, token, id, relation stri } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Share failed to complete successfully", args...) + lm.logger.Warn("Share thing failed to complete successfully", args...) return } - lm.logger.Info("Share completed successfully", args...) + lm.logger.Info("Share thing completed successfully", args...) }(time.Now()) return lm.svc.Share(ctx, token, id, relation, userids...) } @@ -272,10 +281,10 @@ func (lm *loggingMiddleware) Unshare(ctx context.Context, token, id, relation st } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Unshare failed to complete successfully", args...) + lm.logger.Warn("Unshare thing failed to complete successfully", args...) return } - lm.logger.Info("Unshare completed successfully", args...) + lm.logger.Info("Unshare thing completed successfully", args...) }(time.Now()) return lm.svc.Unshare(ctx, token, id, relation, userids...) } diff --git a/twins/api/logging.go b/twins/api/logging.go index 6867eda78..fd23556c1 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -34,7 +34,7 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi "twin", slog.String("id", tw.ID), slog.String("name", tw.Name), - slog.Group("definition", slog.Any("def_id", def.ID)), + slog.Any("definitions", tw.Definitions), ), } if err != nil { @@ -56,7 +56,7 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin "twin", slog.String("id", twin.ID), slog.String("name", twin.Name), - slog.Group("definition", slog.Any("def_id", def.ID)), + slog.Any("definitions", def), ), } if err != nil { @@ -96,6 +96,7 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset slog.String("name", name), slog.Uint64("offset", offset), slog.Uint64("limit", limit), + slog.Uint64("total", page.Total), ), } if err != nil { @@ -115,9 +116,9 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess slog.String("duration", time.Since(begin).String()), slog.Group( "message", - slog.String("subtopic", msg.Subtopic), - slog.String("channel", msg.Channel), - slog.String("publisher", msg.Publisher), + slog.String("subtopic", msg.GetSubtopic()), + slog.String("channel", msg.GetChannel()), + slog.String("publisher", msg.GetPublisher()), ), } if err != nil { @@ -140,6 +141,7 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse "page", slog.Uint64("offset", offset), slog.Uint64("limit", limit), + slog.Uint64("total", page.Total), ), } if err != nil { diff --git a/users/api/logging.go b/users/api/logging.go index b734f0279..800719be1 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -5,7 +5,6 @@ package api import ( "context" - "fmt" "log/slog" "time" @@ -169,7 +168,7 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.Any("tags", fmt.Sprintf("%v", c.Tags))), + slog.Group("user", slog.String("id", c.ID), slog.Any("tags", c.Tags)), } if err != nil { args = append(args, slog.Any("error", err)) @@ -294,7 +293,11 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", c.ID), + slog.Group( + "user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -312,7 +315,11 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", c.ID), + slog.Group( + "user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -330,7 +337,10 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("object", slog.String("kind", objectKind), slog.String("id", objectID)), + slog.Group("object", + slog.String("kind", objectKind), + slog.String("id", objectID), + ), slog.Group( "page", slog.Uint64("limit", cp.Limit), @@ -356,10 +366,10 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id str } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Identify failed to complete successfully", args...) + lm.logger.Warn("Identify user failed to complete successfully", args...) return } - lm.logger.Info("Identify completed successfully", args...) + lm.logger.Info("Identify user completed successfully", args...) }(time.Now()) return lm.svc.Identify(ctx, token) } From c1c88866e9faf7b4c7c78a02f6f07a51b67e2575 Mon Sep 17 00:00:00 2001 From: Musilah Date: Wed, 24 Jan 2024 19:33:39 +0300 Subject: [PATCH 23/23] update log messages and policy requests Signed-off-by: Musilah --- auth/api/logging.go | 114 +++++++++-------------------- bootstrap/api/logging.go | 49 +++++++------ certs/api/logging.go | 6 +- consumers/notifiers/api/logging.go | 15 ++-- consumers/writers/api/logging.go | 4 +- internal/groups/api/logging.go | 35 ++++----- invitations/middleware/logging.go | 58 +++++++++++---- lora/api/logging.go | 55 ++++++-------- opcua/api/logging.go | 50 ++++++------- provision/api/logging.go | 1 + readers/api/logging.go | 3 +- things/api/logging.go | 40 +++++----- twins/api/logging.go | 17 ++--- users/api/logging.go | 64 +++++++++++----- 14 files changed, 240 insertions(+), 271 deletions(-) diff --git a/auth/api/logging.go b/auth/api/logging.go index 871485f3e..a99fd2a92 100644 --- a/auth/api/logging.go +++ b/auth/api/logging.go @@ -7,6 +7,7 @@ package api import ( "context" + "fmt" "log/slog" "time" @@ -46,13 +47,12 @@ func (lm *loggingMiddleware) ListAllObjects(ctx context.Context, pr auth.PolicyR defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", + slog.Group("policy_request", slog.String("object_type", pr.ObjectType), slog.String("subject_id", pr.Subject), slog.String("subject_type", pr.SubjectType), + slog.String("permission", pr.Permission), ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -69,12 +69,6 @@ func (lm *loggingMiddleware) CountObjects(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "object", - slog.String("id", pr.Object), - slog.String("type", pr.ObjectType), - ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -90,13 +84,6 @@ func (lm *loggingMiddleware) ListSubjects(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", - slog.String("subject_type", pr.SubjectType), - slog.String("object_id", pr.Object), - slog.String("object_type", pr.ObjectType), - ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -113,13 +100,12 @@ func (lm *loggingMiddleware) ListAllSubjects(ctx context.Context, pr auth.Policy defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", + slog.Group("policy_request", slog.String("sybject_type", pr.SubjectType), slog.String("object_id", pr.Object), slog.String("object_type", pr.ObjectType), + slog.String("permission", pr.Permission), ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -136,11 +122,6 @@ func (lm *loggingMiddleware) CountSubjects(ctx context.Context, pr auth.PolicyRe defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "subject", - slog.String("id", pr.Subject), - slog.String("type", pr.SubjectType), - ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -157,8 +138,7 @@ func (lm *loggingMiddleware) ListPermissions(ctx context.Context, pr auth.Policy args := []any{ slog.String("duration", time.Since(begin).String()), slog.Any("filter_permissions", filterPermissions), - slog.Group( - "policy_request", + slog.Group("policy_request", slog.String("object_id", pr.Object), slog.String("object_type", pr.ObjectType), slog.String("subject_id", pr.Subject), @@ -180,15 +160,11 @@ func (lm *loggingMiddleware) Issue(ctx context.Context, token string, key auth.K defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "key", - slog.String("id", key.ID), + slog.Group("key", + slog.String("subject", key.Subject), slog.Any("type", key.Type), ), } - if key.Type != auth.AccessKey && !key.ExpiresAt.IsZero() { - args = append(args, slog.Any("expiration_date", key.ExpiresAt)) - } if err != nil { args = append(args, slog.Any("error", err)) lm.logger.Warn("Issue key failed to complete successfully", args...) @@ -204,7 +180,7 @@ func (lm *loggingMiddleware) Revoke(ctx context.Context, token, id string) (err defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("key_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -221,7 +197,7 @@ func (lm *loggingMiddleware) RetrieveKey(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("key_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -238,6 +214,10 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id aut defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), + slog.Group("key", + slog.String("subject", id.Subject), + slog.Any("type", id.Type), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -254,14 +234,13 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, pr auth.PolicyReq) ( defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "object", + slog.Group("object", slog.String("id", pr.Object), slog.String("type", pr.ObjectType), ), - slog.Group( - "subject", + slog.Group("subject", slog.String("id", pr.Subject), + slog.String("kind", pr.SubjectKind), slog.String("type", pr.SubjectType), ), slog.String("permission", pr.Permission), @@ -280,14 +259,13 @@ func (lm *loggingMiddleware) AddPolicy(ctx context.Context, pr auth.PolicyReq) ( defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", + slog.Group("policy_request", slog.String("object_id", pr.Object), slog.String("object_type", pr.ObjectType), slog.String("subject_id", pr.Subject), slog.String("subject_type", pr.SubjectType), + slog.String("relation", pr.Relation), ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -303,21 +281,13 @@ func (lm *loggingMiddleware) AddPolicies(ctx context.Context, prs []auth.PolicyR defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", - slog.String("subject_id", prs[0].Subject), - slog.String("subject_type", prs[0].SubjectType), - slog.String("object_id", prs[0].Object), - slog.String("object_type", prs[0].ObjectType), - ), - slog.String("permission", prs[0].Permission), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Add policies failed to complete successfully", args...) + lm.logger.Warn(fmt.Sprintf("Add %d policies failed to complete successfully", len(prs)), args...) return } - lm.logger.Info("Add policies completed successfully", args...) + lm.logger.Info(fmt.Sprintf("Add %d policies completed successfully", len(prs)), args...) }(time.Now()) return lm.svc.AddPolicies(ctx, prs) @@ -327,14 +297,13 @@ func (lm *loggingMiddleware) DeletePolicy(ctx context.Context, pr auth.PolicyReq defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", + slog.Group("policy_request", slog.String("object_id", pr.Object), slog.String("object_type", pr.ObjectType), slog.String("subject_id", pr.Subject), slog.String("subject_type", pr.SubjectType), + slog.String("relation", pr.Relation), ), - slog.String("permission", pr.Permission), } if err != nil { args = append(args, slog.Any("error", err)) @@ -350,21 +319,13 @@ func (lm *loggingMiddleware) DeletePolicies(ctx context.Context, prs []auth.Poli defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "policy_request", - slog.String("subject_id", prs[0].Subject), - slog.String("subject_type", prs[0].SubjectType), - slog.String("object_id", prs[0].Object), - slog.String("object_type", prs[0].ObjectType), - ), - slog.String("permission", prs[0].Permission), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Delete policies failed to complete successfully", args...) + lm.logger.Warn(fmt.Sprintf("Delete %d policies failed to complete successfully", len(prs)), args...) return } - lm.logger.Info("Delete policies completed successfully", args...) + lm.logger.Info(fmt.Sprintf("Delete %d policies completed successfully", len(prs)), args...) }(time.Now()) return lm.svc.DeletePolicies(ctx, prs) } @@ -373,8 +334,7 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "domain", + slog.Group("domain", slog.String("id", d.ID), slog.String("name", d.Name), ), @@ -425,8 +385,7 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "domain", + slog.Group("domain", slog.String("id", id), slog.Any("name", d.Name), ), @@ -445,9 +404,9 @@ func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id s defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "domain", + slog.Group("domain", slog.String("id", id), + slog.String("name", do.Name), slog.Any("status", d.Status), ), } @@ -465,8 +424,7 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "page", + slog.Group("page", slog.Uint64("limit", page.Limit), slog.Uint64("offset", page.Offset), slog.Uint64("total", page.Total), @@ -486,16 +444,16 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("domain_id", id), slog.String("relation", relation), slog.Any("user_ids", userIds), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Assign users failed to complete successfully", args...) + lm.logger.Warn("Assign users to domain failed to complete successfully", args...) return } - lm.logger.Info("Assign users completed successfully", args...) + lm.logger.Info("Assign users to domain completed successfully", args...) }(time.Now()) return lm.svc.AssignUsers(ctx, token, id, userIds, relation) } @@ -504,16 +462,16 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("domain_id", id), slog.String("relation", relation), slog.Any("user_ids", userIds), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Unassign users failed to complete successfully", args...) + lm.logger.Warn("Unassign users to domain failed to complete successfully", args...) return } - lm.logger.Info("Unassign users completed successfully", args...) + lm.logger.Info("Unassign users to domain completed successfully", args...) }(time.Now()) return lm.svc.UnassignUsers(ctx, token, id, userIds, relation) } diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 0d43d8fbb..53fb12c4d 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -35,10 +35,10 @@ func (lm *loggingMiddleware) Add(ctx context.Context, token string, cfg bootstra } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Add new Thing config failed to complete successfully", args...) + lm.logger.Warn("Add new bootstrap failed to complete successfully", args...) return } - lm.logger.Info("Add new Thing config completed successfully", args...) + lm.logger.Info("Add new bootstrap completed successfully", args...) }(time.Now()) return lm.svc.Add(ctx, token, cfg) @@ -69,14 +69,17 @@ func (lm *loggingMiddleware) Update(ctx context.Context, token string, cfg boots defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("thing_id", cfg.ThingID), + slog.Group("config", + slog.String("thing_id", cfg.ThingID), + slog.String("name", cfg.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update config failed to complete successfully", args...) + lm.logger.Warn("Update boostrap config failed to complete successfully", args...) return } - lm.logger.Info("Update config completed successfully", args...) + lm.logger.Info("Update boostrap config completed successfully", args...) }(time.Now()) return lm.svc.Update(ctx, token, cfg) @@ -92,10 +95,10 @@ func (lm *loggingMiddleware) UpdateCert(ctx context.Context, token, thingID, cli } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update cert failed to complete successfully", args...) + lm.logger.Warn("Update bootstrap config certificate failed to complete successfully", args...) return } - lm.logger.Info("Update cert completed successfully", args...) + lm.logger.Info("Update bootstrap config certificate completed successfully", args...) }(time.Now()) return lm.svc.UpdateCert(ctx, token, thingID, clientCert, clientKey, caCert) @@ -108,13 +111,14 @@ func (lm *loggingMiddleware) UpdateConnections(ctx context.Context, token, id st args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", id), + slog.Any("connections", connections), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update connections failed to complete successfully", args...) + lm.logger.Warn("Update config connections failed to complete successfully", args...) return } - lm.logger.Info("Update connections completed successfully", args...) + lm.logger.Info("Update config connections completed successfully", args...) }(time.Now()) return lm.svc.UpdateConnections(ctx, token, id, connections) @@ -126,10 +130,11 @@ func (lm *loggingMiddleware) List(ctx context.Context, token string, filter boot defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "filter", + slog.Group("page", + slog.Any("filter", filter), slog.Uint64("offset", offset), slog.Uint64("limit", limit), + slog.Uint64("total", res.Total), ), } if err != nil { @@ -149,14 +154,14 @@ func (lm *loggingMiddleware) Remove(ctx context.Context, token, id string) (err defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("thing_id", id), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove config failed to complete successfully", args...) + lm.logger.Warn("Remove bootstrap config failed to complete successfully", args...) return } - lm.logger.Info("Remove config completed successfully", args...) + lm.logger.Info("Remove bootstrap config completed successfully", args...) }(time.Now()) return lm.svc.Remove(ctx, token, id) @@ -166,17 +171,14 @@ func (lm *loggingMiddleware) Bootstrap(ctx context.Context, externalKey, externa defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "config", - slog.String("external_id", externalID), - ), + slog.String("external_id", externalID), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Bootstrap failed to complete successfully", args...) + lm.logger.Warn("View bootstrap config failed to complete successfully", args...) return } - lm.logger.Info("Bootstrap completed successfully", args...) + lm.logger.Info("View bootstrap completed successfully", args...) }(time.Now()) return lm.svc.Bootstrap(ctx, externalKey, externalID, secure) @@ -191,10 +193,10 @@ func (lm *loggingMiddleware) ChangeState(ctx context.Context, token, id string, } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Change state failed to complete successfully", args...) + lm.logger.Warn("Change thing state failed to complete successfully", args...) return } - lm.logger.Info("Change state completed successfully", args...) + lm.logger.Info("Change thing state completed successfully", args...) }(time.Now()) return lm.svc.ChangeState(ctx, token, id, state) @@ -204,8 +206,7 @@ func (lm *loggingMiddleware) UpdateChannelHandler(ctx context.Context, channel b defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "channel", + slog.Group("channel", slog.String("id", channel.ID), slog.String("name", channel.Name), slog.Any("metadata", channel.Metadata), diff --git a/certs/api/logging.go b/certs/api/logging.go index d7f892540..e83e0ac95 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -51,8 +51,7 @@ func (lm *loggingMiddleware) ListCerts(ctx context.Context, token, thingID strin args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("offset", cp.Offset), slog.Uint64("limit", cp.Limit), slog.Uint64("total", cp.Total), @@ -76,8 +75,7 @@ func (lm *loggingMiddleware) ListSerials(ctx context.Context, token, thingID str args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("thing_id", thingID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("offset", cp.Offset), slog.Uint64("limit", cp.Limit), slog.Uint64("total", cp.Total), diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index 91b2c4562..50b3bffa2 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -31,8 +31,7 @@ func (lm *loggingMiddleware) CreateSubscription(ctx context.Context, token strin defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "subscription", + slog.Group("subscription", slog.String("topic", sub.Topic), slog.String("id", id), ), @@ -54,8 +53,7 @@ func (lm *loggingMiddleware) ViewSubscription(ctx context.Context, token, topic defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "subscription", + slog.Group("subscription", slog.String("topic", topic), slog.String("id", sub.ID), ), @@ -77,8 +75,7 @@ func (lm *loggingMiddleware) ListSubscriptions(ctx context.Context, token string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "page", + slog.Group("page", slog.String("topic", pm.Topic), slog.Int("limit", pm.Limit), slog.Uint64("offset", uint64(pm.Offset)), @@ -102,7 +99,7 @@ func (lm *loggingMiddleware) RemoveSubscription(ctx context.Context, token, id s defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.String("id", id), + slog.String("subscription_id", id), } if err != nil { args = append(args, slog.Any("error", err)) @@ -124,10 +121,10 @@ func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msg interface{ } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Consume blocking failed to complete successfully", args...) + lm.logger.Warn("Blocking consumer failed to consume messages successfully", args...) return } - lm.logger.Info("Consume blocking completed successfully", args...) + lm.logger.Info("Blocking consumer consumed messages successfully", args...) }(time.Now()) return lm.svc.ConsumeBlocking(ctx, msg) diff --git a/consumers/writers/api/logging.go b/consumers/writers/api/logging.go index bba75a595..77e5f9144 100644 --- a/consumers/writers/api/logging.go +++ b/consumers/writers/api/logging.go @@ -37,10 +37,10 @@ func (lm *loggingMiddleware) ConsumeBlocking(ctx context.Context, msgs interface } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Consume blocking failed to complete successfully", args...) + lm.logger.Warn("Blocking consumer failed to consume messages successfully", args...) return } - lm.logger.Info("Consume blocking completed successfully", args...) + lm.logger.Info("Blocking consumer consumed messages successfully", args...) }(time.Now()) return lm.consumer.ConsumeBlocking(ctx, msgs) diff --git a/internal/groups/api/logging.go b/internal/groups/api/logging.go index f57e4e1cd..5f7befc15 100644 --- a/internal/groups/api/logging.go +++ b/internal/groups/api/logging.go @@ -29,8 +29,7 @@ func (lm *loggingMiddleware) CreateGroup(ctx context.Context, token, kind string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", + slog.Group("group", slog.String("id", g.ID), slog.String("name", g.Name), ), @@ -51,11 +50,10 @@ func (lm *loggingMiddleware) UpdateGroup(ctx context.Context, token string, grou defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", - slog.String("id", g.ID), - slog.String("name", g.Name), - slog.Any("metadata", g.Metadata), + slog.Group("group", + slog.String("id", group.ID), + slog.String("name", group.Name), + slog.Any("metadata", group.Metadata), ), } if err != nil { @@ -74,8 +72,7 @@ func (lm *loggingMiddleware) ViewGroup(ctx context.Context, token, id string) (g defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", + slog.Group("group", slog.String("id", g.ID), slog.String("name", g.Name), ), @@ -114,13 +111,11 @@ func (lm *loggingMiddleware) ListGroups(ctx context.Context, token, memberKind, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "member", + slog.Group("member", slog.String("id", memberID), slog.String("kind", memberKind), ), - slog.Group( - "page", + slog.Group("page", slog.Uint64("limit", gp.Limit), slog.Uint64("offset", gp.Offset), slog.Uint64("total", cg.Total), @@ -142,8 +137,7 @@ func (lm *loggingMiddleware) EnableGroup(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", + slog.Group("group", slog.String("id", id), slog.String("name", g.Name), ), @@ -164,8 +158,7 @@ func (lm *loggingMiddleware) DisableGroup(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "group", + slog.Group("group", slog.String("id", id), slog.String("name", g.Name), ), @@ -211,10 +204,10 @@ func (lm *loggingMiddleware) Assign(ctx context.Context, token, groupID, relatio } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Assign member failed to complete successfully", args...) + lm.logger.Warn("Assign member to group failed to complete successfully", args...) return } - lm.logger.Info("Assign member completed successfully", args...) + lm.logger.Info("Assign member to group completed successfully", args...) }(time.Now()) return lm.svc.Assign(ctx, token, groupID, relation, memberKind, memberIDs...) @@ -231,10 +224,10 @@ func (lm *loggingMiddleware) Unassign(ctx context.Context, token, groupID, relat } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Unassign member failed to complete successfully", args...) + lm.logger.Warn("Unassign member to group failed to complete successfully", args...) return } - lm.logger.Info("Unassign member completed successfully", args...) + lm.logger.Info("Unassign member to group completed successfully", args...) }(time.Now()) return lm.svc.Unassign(ctx, token, groupID, relation, memberKind, memberIDs...) diff --git a/invitations/middleware/logging.go b/invitations/middleware/logging.go index 49524a9c9..6acf135ee 100644 --- a/invitations/middleware/logging.go +++ b/invitations/middleware/logging.go @@ -5,7 +5,6 @@ package middleware import ( "context" - "fmt" "log/slog" "time" @@ -25,60 +24,87 @@ func Logging(logger *slog.Logger, svc invitations.Service) invitations.Service { func (lm *logging) SendInvitation(ctx context.Context, token string, invitation invitations.Invitation) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method send_invitation to user_id %s from domain_id %s took %s to complete", invitation.UserID, invitation.DomainID, time.Since(begin)) + args := []any{ + slog.String("duration", time.Since(begin).String()), + slog.String("user_id", invitation.UserID), + slog.String("domain_id", invitation.DomainID), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("Send invitation failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("Send invitation completed successfully", args...) }(time.Now()) return lm.svc.SendInvitation(ctx, token, invitation) } func (lm *logging) ViewInvitation(ctx context.Context, token, userID, domainID string) (invitation invitations.Invitation, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method view_invitation took %s to complete", time.Since(begin)) + args := []any{ + slog.String("duration", time.Since(begin).String()), + slog.String("user_id", userID), + slog.String("domain_id", domainID), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("View invitation failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("View invitation completed successfully", args...) }(time.Now()) return lm.svc.ViewInvitation(ctx, token, userID, domainID) } func (lm *logging) ListInvitations(ctx context.Context, token string, page invitations.Page) (invs invitations.InvitationPage, err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method list_invitations took %s to complete", time.Since(begin)) + args := []any{ + slog.String("duration", time.Since(begin).String()), + slog.Group("page", + slog.Uint64("offset", page.Offset), + slog.Uint64("limit", page.Limit), + slog.Uint64("total", invs.Total), + ), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("List invitations failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("List invitations completed successfully", args...) }(time.Now()) return lm.svc.ListInvitations(ctx, token, page) } func (lm *logging) AcceptInvitation(ctx context.Context, token, domainID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method accept_invitation took %s to complete", time.Since(begin)) + args := []any{ + slog.String("duration", time.Since(begin).String()), + slog.String("domain_id", domainID), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("Accept invitation failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("Accept invitation completed successfully", args...) }(time.Now()) return lm.svc.AcceptInvitation(ctx, token, domainID) } func (lm *logging) DeleteInvitation(ctx context.Context, token, userID, domainID string) (err error) { defer func(begin time.Time) { - message := fmt.Sprintf("Method delete_invitation took %s to complete", time.Since(begin)) + args := []any{ + slog.String("duration", time.Since(begin).String()), + slog.String("user_id", userID), + slog.String("domain_id", domainID), + } if err != nil { - lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) + args = append(args, slog.Any("error", err)) + lm.logger.Warn("Delete invitation failed to complete successfully", args...) return } - lm.logger.Info(fmt.Sprintf("%s without errors.", message)) + lm.logger.Info("Delete invitation completed successfully", args...) }(time.Now()) return lm.svc.DeleteInvitation(ctx, token, userID, domainID) } diff --git a/lora/api/logging.go b/lora/api/logging.go index dae317330..8190e700c 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -32,18 +32,15 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, thingID, loraDevEUI defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", thingID), - ), + slog.String("thing_id", thingID), slog.String("dev_eui", loraDevEUI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thing %s and lora-dev-eui %s failed to complete successfully", args...) + lm.logger.Warn("Create thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Create thing %s and lora-dev-eui %s completed successfully", args...) + lm.logger.Info("Create thing route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, thingID, loraDevEUI) @@ -53,18 +50,15 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, thingID, loraDevEUI defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", thingID), - ), + slog.String("thing_id", thingID), slog.String("dev_eui", loraDevEUI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thing %s and lora-dev-eui %s failed to complete successfully", args...) + lm.logger.Warn("Update thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Update thing %s and lora-dev-eui %s completed successfully", args...) + lm.logger.Info("Update thing route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, thingID, loraDevEUI) @@ -78,10 +72,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, thingID string) (er } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thing failed to complete successfully", args...) + lm.logger.Warn("Remove thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove thing completed successfully", args...) + lm.logger.Info("Remove thing route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, thingID) @@ -91,18 +85,15 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, chanID, loraApp s defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "channel", - slog.String("id", chanID), - ), + slog.String("channel_id", chanID), slog.String("lora_app", loraApp), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channel %s and lora-app %s failed to complete successfully", args...) + lm.logger.Warn("Create channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Create channel %s and lora-app %s completed successfully", args...) + lm.logger.Info("Create channel route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, chanID, loraApp) @@ -112,17 +103,14 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, chanID, loraApp s defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "channel", - slog.String("id", chanID), - ), + slog.String("channel_id", chanID), slog.String("lora_app", loraApp), } if err != nil { - lm.logger.Warn("Update channel %s and lora-app %s failed to complete successfully", args...) + lm.logger.Warn("Update channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Update channel %s and lora-app %s route-map completed successfully", args...) + lm.logger.Info("Update channel route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, chanID, loraApp) @@ -135,10 +123,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, chanID string) (e slog.String("channel_id", chanID), } if err != nil { - lm.logger.Warn("Remove channel failed to complete successfully", args...) + lm.logger.Warn("Remove channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove channel completed successfully", args...) + lm.logger.Info("Remove channel route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, chanID) @@ -153,10 +141,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, chanID, thingID st } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Connect thing for channel %s and thing %s failed to complete successfully", args...) + lm.logger.Warn("Connect thing to channel failed to complete successfully", args...) return } - lm.logger.Info("Connect thing for channel %s and thing %s completed successfully", args...) + lm.logger.Info("Connect thing to channel completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, chanID, thingID) @@ -171,10 +159,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, chanID, thingID } if err != nil { args := append(args, slog.String("error", err.Error())) - lm.logger.Warn("Disconnect thing for channel %s and thing %s failed to complete successfully", args...) + lm.logger.Warn("Disconnect thing from channel failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thing for channel %s and thing %s completed successfully", args...) + lm.logger.Info("Disconnect thing from channel completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, chanID, thingID) @@ -184,8 +172,7 @@ func (lm loggingMiddleware) Publish(ctx context.Context, msg *lora.Message) (err defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "message", + slog.Group("message", slog.String("application_id", msg.ApplicationID), slog.String("device_eui", msg.DevEUI), ), diff --git a/opcua/api/logging.go b/opcua/api/logging.go index 1b5dd44c7..e58e97abb 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -37,10 +37,10 @@ func (lm loggingMiddleware) CreateThing(ctx context.Context, mgxThing, opcuaNode } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thing %s with nodeID %s failed to complete successfully", args...) + lm.logger.Warn("Create thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Create thing %s with nodeID %s completed successfully", args...) + lm.logger.Info("Create thing route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateThing(ctx, mgxThing, opcuaNodeID) @@ -55,10 +55,10 @@ func (lm loggingMiddleware) UpdateThing(ctx context.Context, mgxThing, opcuaNode } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update thing %s with nodeID %s failed to complete successfully", args...) + lm.logger.Warn("Update thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Update thing %s with nodeID %s completed successfully", args...) + lm.logger.Info("Update thing route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateThing(ctx, mgxThing, opcuaNodeID) @@ -72,10 +72,10 @@ func (lm loggingMiddleware) RemoveThing(ctx context.Context, mgxThing string) (e } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove thing failed to complete successfully", args...) + lm.logger.Warn("Remove thing route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove thing completed successfully", args...) + lm.logger.Info("Remove thing route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveThing(ctx, mgxThing) @@ -85,18 +85,15 @@ func (lm loggingMiddleware) CreateChannel(ctx context.Context, mgxChan, opcuaSer defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "channel", - slog.String("id", mgxChan), - slog.String("server_uri", opcuaServerURI), - ), + slog.String("channel_id", mgxChan), + slog.String("server_uri", opcuaServerURI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create channel %s with ServerURI %s failed to complete successfully", args...) + lm.logger.Warn("Create channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Create channel %s with ServerURI %s completed successfully", args...) + lm.logger.Info("Create channel route-map completed successfully", args...) }(time.Now()) return lm.svc.CreateChannel(ctx, mgxChan, opcuaServerURI) @@ -106,18 +103,15 @@ func (lm loggingMiddleware) UpdateChannel(ctx context.Context, mgxChanID, opcuaS defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "channel", - slog.String("id", mgxChanID), - slog.String("server_uri", opcuaServerURI), - ), + slog.String("channel_id", mgxChanID), + slog.String("server_uri", opcuaServerURI), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Update channel %s with ServerURI %s failed to complete successfully", args...) + lm.logger.Warn("Update channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Update channel %s with ServerURI %s completed successfully", args...) + lm.logger.Info("Update channel route-map completed successfully", args...) }(time.Now()) return lm.svc.UpdateChannel(ctx, mgxChanID, opcuaServerURI) @@ -131,10 +125,10 @@ func (lm loggingMiddleware) RemoveChannel(ctx context.Context, mgxChanID string) } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Remove channel %s failed to complete successfully", args...) + lm.logger.Warn("Remove channel route-map failed to complete successfully", args...) return } - lm.logger.Info("Remove channel %s completed successfully", args...) + lm.logger.Info("Remove channel route-map completed successfully", args...) }(time.Now()) return lm.svc.RemoveChannel(ctx, mgxChanID) @@ -149,10 +143,10 @@ func (lm loggingMiddleware) ConnectThing(ctx context.Context, mgxChanID, mgxThin } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Connect thing for channel %s and thing %s failed to complete successfully", args...) + lm.logger.Warn("Connect thing to channel failed to complete successfully", args...) return } - lm.logger.Info("Connect thing for channel %s and thing %s completed successfully", args...) + lm.logger.Info("Connect thing to channel completed successfully", args...) }(time.Now()) return lm.svc.ConnectThing(ctx, mgxChanID, mgxThingID) @@ -167,10 +161,10 @@ func (lm loggingMiddleware) DisconnectThing(ctx context.Context, mgxChanID, mgxT } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Disconnect thing for channel %s and thing %s failed to complete successfully", args...) + lm.logger.Warn("Disconnect thing from channel failed to complete successfully", args...) return } - lm.logger.Info("Disconnect thing for channel %s and thing %s completed successfully", args...) + lm.logger.Info("Disconnect thing from channel completed successfully", args...) }(time.Now()) return lm.svc.DisconnectThing(ctx, mgxChanID, mgxThingID) @@ -186,10 +180,10 @@ func (lm loggingMiddleware) Browse(ctx context.Context, serverURI, namespace, id } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Browse failed to complete successfully", args...) + lm.logger.Warn("Browse available nodes failed to complete successfully", args...) return } - lm.logger.Info("Browse completed successfully", args...) + lm.logger.Info("Browse available nodes completed successfully", args...) }(time.Now()) return lm.svc.Browse(ctx, serverURI, namespace, identifier) diff --git a/provision/api/logging.go b/provision/api/logging.go index f6f6a9f63..c6aa58fbc 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -28,6 +28,7 @@ func (lm *loggingMiddleware) Provision(token, name, externalID, externalKey stri defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), + slog.String("name", name), slog.String("external_id", externalID), } if err != nil { diff --git a/readers/api/logging.go b/readers/api/logging.go index c7ea18045..614337df9 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -32,8 +32,7 @@ func (lm *loggingMiddleware) ReadAll(chanID string, rpm readers.PageMetadata) (p args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", chanID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("offset", rpm.Offset), slog.Uint64("limit", rpm.Limit), slog.Uint64("total", page.Total), diff --git a/things/api/logging.go b/things/api/logging.go index f78b46e6c..86ae56803 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -5,6 +5,7 @@ package api import ( "context" + "fmt" "log/slog" "time" @@ -28,14 +29,13 @@ func (lm *loggingMiddleware) CreateThings(ctx context.Context, token string, cli defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Any("no_of_things", len(clients)), } if err != nil { args = append(args, slog.Any("error", err)) - lm.logger.Warn("Create thing failed to complete successfully", args...) + lm.logger.Warn(fmt.Sprintf("Create %d things failed to complete successfully", len(clients)), args...) return } - lm.logger.Info("Create thing completed successfully", args...) + lm.logger.Info(fmt.Sprintf("Create %d things completed successfully", len(clients)), args...) }(time.Now()) return lm.svc.CreateThings(ctx, token, clients...) } @@ -44,7 +44,10 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("thing", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.Group("thing", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -77,8 +80,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token, reqUserID s args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("user_id", reqUserID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("limit", pm.Limit), slog.Uint64("offset", pm.Offset), slog.Uint64("total", cp.Total), @@ -98,8 +100,7 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", + slog.Group("thing", slog.String("id", client.ID), slog.String("name", client.Name), slog.Any("metadata", client.Metadata), @@ -119,10 +120,10 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("id", client.ID), - slog.Any("tags", client.Tags), + slog.Group("thing", + slog.String("id", c.ID), + slog.String("name", c.Name), + slog.Any("tags", c.Tags), ), } if err != nil { @@ -139,8 +140,7 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", + slog.Group("thing", slog.String("id", c.ID), slog.String("name", c.Name), ), @@ -159,8 +159,7 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", + slog.Group("thing", slog.String("id", id), slog.String("name", c.Name), ), @@ -179,9 +178,8 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "thing", - slog.String("thing_id", id), + slog.Group("thing", + slog.String("id", id), slog.String("name", c.Name), ), } @@ -200,8 +198,7 @@ func (lm *loggingMiddleware) ListClientsByGroup(ctx context.Context, token, chan args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("channel_id", channelID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("offset", cp.Offset), slog.Uint64("limit", cp.Limit), slog.Uint64("total", mp.Total), @@ -239,7 +236,6 @@ func (lm *loggingMiddleware) Authorize(ctx context.Context, req *magistrala.Auth slog.String("duration", time.Since(begin).String()), slog.String("object", req.GetObject()), slog.String("object_type", req.GetObjectType()), - slog.String("subject", req.GetSubject()), slog.String("subject_type", req.GetSubjectType()), slog.String("permission", req.GetPermission()), } diff --git a/twins/api/logging.go b/twins/api/logging.go index fd23556c1..eff62e13a 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -30,8 +30,7 @@ func (lm *loggingMiddleware) AddTwin(ctx context.Context, token string, twin twi defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "twin", + slog.Group("twin", slog.String("id", tw.ID), slog.String("name", tw.Name), slog.Any("definitions", tw.Definitions), @@ -52,8 +51,7 @@ func (lm *loggingMiddleware) UpdateTwin(ctx context.Context, token string, twin defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "twin", + slog.Group("twin", slog.String("id", twin.ID), slog.String("name", twin.Name), slog.Any("definitions", def), @@ -91,8 +89,7 @@ func (lm *loggingMiddleware) ListTwins(ctx context.Context, token string, offset defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "page", + slog.Group("page", slog.String("name", name), slog.Uint64("offset", offset), slog.Uint64("limit", limit), @@ -114,10 +111,9 @@ func (lm *loggingMiddleware) SaveStates(ctx context.Context, msg *messaging.Mess defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "message", - slog.String("subtopic", msg.GetSubtopic()), + slog.Group("message", slog.String("channel", msg.GetChannel()), + slog.String("subtopic", msg.GetSubtopic()), slog.String("publisher", msg.GetPublisher()), ), } @@ -137,8 +133,7 @@ func (lm *loggingMiddleware) ListStates(ctx context.Context, token string, offse args := []any{ slog.String("duration", time.Since(begin).String()), slog.String("twin_id", twinID), - slog.Group( - "page", + slog.Group("page", slog.Uint64("offset", offset), slog.Uint64("limit", limit), slog.Uint64("total", page.Total), diff --git a/users/api/logging.go b/users/api/logging.go index 800719be1..70b693694 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -31,7 +31,10 @@ func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, c defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -52,7 +55,7 @@ func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret, d slog.String("domain_id", domainID), } if t.AccessType != "" { - args = append(args, "access_type", t.AccessType) + args = append(args, slog.String("access_type", t.AccessType)) } if err != nil { args = append(args, slog.Any("error", err)) @@ -73,7 +76,7 @@ func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken, dom slog.String("domain_id", domainID), } if t.AccessType != "" { - args = append(args, "access_type", t.AccessType) + args = append(args, slog.String("access_type", t.AccessType)) } if err != nil { args = append(args, slog.Any("error", err)) @@ -91,7 +94,10 @@ func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) ( defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", id), slog.String("name", c.Name)), + slog.Group("user", + slog.String("id", id), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -109,7 +115,10 @@ func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c m defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -127,8 +136,7 @@ func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm m defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "page", + slog.Group("page", slog.Uint64("limit", pm.Limit), slog.Uint64("offset", pm.Offset), slog.Uint64("total", cp.Total), @@ -150,7 +158,11 @@ func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, cli defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID)), slog.String("name", c.Name), slog.Any("metadata", c.Metadata), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + slog.Any("metadata", c.Metadata), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -168,7 +180,11 @@ func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.Any("tags", c.Tags)), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + slog.Any("tags", c.Tags), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -186,7 +202,10 @@ func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID)), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -204,7 +223,10 @@ func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldS defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("name", c.Name)), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -275,7 +297,11 @@ func (lm *loggingMiddleware) UpdateClientRole(ctx context.Context, token string, defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group("user", slog.String("id", c.ID), slog.String("role", client.Role.String())), + slog.Group("user", + slog.String("id", c.ID), + slog.String("name", c.Name), + slog.String("role", client.Role.String()), + ), } if err != nil { args = append(args, slog.Any("error", err)) @@ -293,9 +319,8 @@ func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "user", - slog.String("id", c.ID), + slog.Group("user", + slog.String("id", id), slog.String("name", c.Name), ), } @@ -315,9 +340,8 @@ func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), - slog.Group( - "user", - slog.String("id", c.ID), + slog.Group("user", + slog.String("id", id), slog.String("name", c.Name), ), } @@ -341,8 +365,7 @@ func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, slog.String("kind", objectKind), slog.String("id", objectID), ), - slog.Group( - "page", + slog.Group("page", slog.Uint64("limit", cp.Limit), slog.Uint64("offset", cp.Offset), slog.Uint64("total", mp.Total), @@ -363,6 +386,7 @@ func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id str defer func(begin time.Time) { args := []any{ slog.String("duration", time.Since(begin).String()), + slog.String("user_id", id), } if err != nil { args = append(args, slog.Any("error", err))