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

Commit

Permalink
update slog arguments for remaining services
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Jan 19, 2024
1 parent 03fa934 commit 4dfb06a
Show file tree
Hide file tree
Showing 13 changed files with 754 additions and 1,027 deletions.
459 changes: 185 additions & 274 deletions auth/api/logging.go

Large diffs are not rendered by default.

271 changes: 110 additions & 161 deletions bootstrap/api/logging.go

Large diffs are not rendered by default.

121 changes: 51 additions & 70 deletions certs/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package api

import (
"context"
"fmt"
"log/slog"
"time"

Expand All @@ -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)
Expand Down
71 changes: 24 additions & 47 deletions coap/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package api

import (
"context"
"fmt"
"log/slog"
"time"

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 4dfb06a

Please sign in to comment.