From 2a452b6691eba1d4714606d20822f7bc3d8b766c Mon Sep 17 00:00:00 2001 From: Musilah Date: Fri, 19 Jan 2024 19:29:04 +0300 Subject: [PATCH] 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)