-
Notifications
You must be signed in to change notification settings - Fork 671
/
logging.go
93 lines (80 loc) · 2.92 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
//go:build !test
package api
import (
"context"
"log/slog"
"time"
"github.com/absmach/magistrala/coap"
"github.com/absmach/magistrala/pkg/messaging"
)
var _ coap.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger *slog.Logger
svc coap.Service
}
// LoggingMiddleware adds logging facilities to the adapter.
func LoggingMiddleware(svc coap.Service, logger *slog.Logger) coap.Service {
return &loggingMiddleware{logger, svc}
}
// Publish logs the publish request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
// 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 := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("channel_id", msg.GetChannel()),
}
if msg.GetSubtopic() != "" {
args = append(args, slog.String("subtopic", msg.GetSubtopic()))
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Publish message failed to complete successfully", args...)
return
}
lm.logger.Info("Publish message completed successfully", args...)
}(time.Now())
return lm.svc.Publish(ctx, key, msg)
}
// Subscribe logs the subscribe request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
// 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 := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("channel_id", chanID),
}
if subtopic != "" {
args = append(args, slog.String("subtopic", subtopic))
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Subscribe failed to complete successfully", args...)
return
}
lm.logger.Info("Subscribe completed successfully", args...)
}(time.Now())
return lm.svc.Subscribe(ctx, key, chanID, subtopic, c)
}
// Unsubscribe logs the unsubscribe request. It logs the channel ID, subtopic (if any) and the time it took to complete the request.
// 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 := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("channel_id", chanID),
}
if subtopic != "" {
args = append(args, slog.String("subtopic", subtopic))
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Unsubscribe failed to complete successfully", args...)
return
}
lm.logger.Info("Unsubscribe completed successfully", args...)
}(time.Now())
return lm.svc.Unsubscribe(ctx, key, chanID, subtopic, token)
}