-
Notifications
You must be signed in to change notification settings - Fork 13
/
logging.go
67 lines (54 loc) · 1.93 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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
//go:build !test
// +build !test
package api
import (
"context"
"fmt"
"time"
log "github.com/MainfluxLabs/mainflux/logger"
"github.com/MainfluxLabs/mainflux/webhooks"
)
var _ webhooks.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger log.Logger
svc webhooks.Service
}
// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc webhooks.Service, logger log.Logger) webhooks.Service {
return &loggingMiddleware{logger, svc}
}
func (lm *loggingMiddleware) CreateWebhooks(ctx context.Context, token string, webhooks ...webhooks.Webhook) (response []webhooks.Webhook, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method create_webhooks took %s to complete", 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.CreateWebhooks(ctx, token, webhooks...)
}
func (lm *loggingMiddleware) ListWebhooksByThing(ctx context.Context, token string, thingID string) (response []webhooks.Webhook, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list_webhooks_by_thing took %s to complete", 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.ListWebhooksByThing(ctx, token, thingID)
}
func (lm *loggingMiddleware) Consume(message interface{}) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method consume took %s to complete", 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.Consume(message)
}