// Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 package api import ( "context" "fmt" "time" "github.com/mainflux/mainflux" mflog "github.com/mainflux/mainflux/logger" mfclients "github.com/mainflux/mainflux/pkg/clients" "github.com/mainflux/mainflux/users" ) var _ users.Service = (*loggingMiddleware)(nil) type loggingMiddleware struct { logger mflog.Logger svc users.Service } // LoggingMiddleware adds logging facilities to the clients service. func LoggingMiddleware(svc users.Service, logger mflog.Logger) users.Service { return &loggingMiddleware{logger, svc} } // RegisterClient logs the register_client request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) RegisterClient(ctx context.Context, token string, client mfclients.Client) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method register_client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.RegisterClient(ctx, token, client) } // IssueToken logs the issue_token request. It logs the client identity and token type and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) IssueToken(ctx context.Context, identity, secret string) (t *mainflux.Token, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method issue_token of type %s for client %s took %s to complete", t.AccessType, identity, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.IssueToken(ctx, identity, secret) } // RefreshToken logs the refresh_token request. It logs the refreshtoken, token type and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) RefreshToken(ctx context.Context, refreshToken string) (t *mainflux.Token, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method refresh_token of type %s for refresh token %s took %s to complete", t.AccessType, refreshToken, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.RefreshToken(ctx, refreshToken) } // ViewClient logs the view_client request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewClient(ctx context.Context, token, id string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method view_client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.ViewClient(ctx, token, id) } // ViewProfile logs the view_profile request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ViewProfile(ctx context.Context, token string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method view_profile with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.ViewProfile(ctx, token) } // ListClients logs the list_clients request. It logs the token and page metadata and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListClients(ctx context.Context, token string, pm mfclients.Page) (cp mfclients.ClientsPage, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method list_clients %d clients using token %s took %s to complete", cp.Total, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.ListClients(ctx, token, pm) } // UpdateClient logs the update_client request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClient(ctx context.Context, token string, client mfclients.Client) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method update_client_name_and_metadata for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.UpdateClient(ctx, token, client) } // UpdateClientTags logs the update_client_tags request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientTags(ctx context.Context, token string, client mfclients.Client) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method update_client_tags for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.UpdateClientTags(ctx, token, client) } // UpdateClientIdentity logs the update_client_identity request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientIdentity(ctx context.Context, token, id, identity string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method update_client_identity for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.UpdateClientIdentity(ctx, token, id, identity) } // UpdateClientSecret logs the update_client_secret request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method update_client_secret for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.UpdateClientSecret(ctx, token, oldSecret, newSecret) } // GenerateResetToken logs the generate_reset_token request. It logs the email and host and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) GenerateResetToken(ctx context.Context, email, host string) (err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method generate_reset_token for email %s and host %s took %s to complete", email, host, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.GenerateResetToken(ctx, email, host) } // ResetSecret logs the reset_secret request. It logs the token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ResetSecret(ctx context.Context, token, secret string) (err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method reset_secret using token %s took %s to complete", token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.ResetSecret(ctx, token, secret) } // SendPasswordReset logs the send_password_reset request. It logs the token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) SendPasswordReset(ctx context.Context, host, email, user, token string) (err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method send_password_reset using token %s took %s to complete", token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.SendPasswordReset(ctx, host, email, user, token) } // UpdateClientOwner logs the update_client_owner request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) UpdateClientOwner(ctx context.Context, token string, client mfclients.Client) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method update_client_owner for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.UpdateClientOwner(ctx, token, client) } // EnableClient logs the enable_client request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) EnableClient(ctx context.Context, token, id string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method enable_client for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.EnableClient(ctx, token, id) } // DisableClient logs the disable_client request. It logs the client id and token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) DisableClient(ctx context.Context, token, id string) (c mfclients.Client, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method disable_client for client with id %s using token %s took %s to complete", c.ID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.DisableClient(ctx, token, id) } // ListMembers logs the list_members request. It logs the group id, token and the time it took to complete the request. // If the request fails, it logs the error. func (lm *loggingMiddleware) ListMembers(ctx context.Context, token, objectKind, objectID string, cp mfclients.Page) (mp mfclients.MembersPage, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method list_members %d members for object kind %s and object id %s and token %s took %s to complete", mp.Total, objectKind, objectID, token, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.ListMembers(ctx, token, objectKind, objectID, cp) } // Identify logs the identify request. It logs the token and the time it took to complete the request. func (lm *loggingMiddleware) Identify(ctx context.Context, token string) (id string, err error) { defer func(begin time.Time) { message := fmt.Sprintf("Method identify for token %s with id %s took %s to complete", token, id, time.Since(begin)) if err != nil { lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err)) return } lm.logger.Info(fmt.Sprintf("%s without errors.", message)) }(time.Now()) return lm.svc.Identify(ctx, token) }