forked from microservices-demo/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
43 lines (38 loc) · 889 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
40
41
42
43
package payment
import (
"github.com/go-kit/kit/log"
"time"
)
// LoggingMiddleware logs method calls, parameters, results, and elapsed time.
func LoggingMiddleware(logger log.Logger) Middleware {
return func(next Service) Service {
return loggingMiddleware{
next: next,
logger: logger,
}
}
}
type loggingMiddleware struct {
next Service
logger log.Logger
}
func (mw loggingMiddleware) Authorise(amount float32) (auth Authorisation, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Authorise",
"result", auth.Authorised,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Authorise(amount)
}
func (mw loggingMiddleware) Health() (health []Health) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Health",
"result", len(health),
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Health()
}