-
Notifications
You must be signed in to change notification settings - Fork 13
/
logging.go
39 lines (30 loc) · 952 Bytes
/
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
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
//go:build !test
package api
import (
"fmt"
"time"
notifiers "github.com/MainfluxLabs/mainflux/consumers/notifiers"
log "github.com/MainfluxLabs/mainflux/logger"
)
var _ notifiers.Service = (*loggingMiddleware)(nil)
type loggingMiddleware struct {
logger log.Logger
svc notifiers.Service
}
// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc notifiers.Service, logger log.Logger) notifiers.Service {
return &loggingMiddleware{logger, svc}
}
func (lm *loggingMiddleware) Consume(msg 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(msg)
}