forked from microservices-demo/catalogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
88 lines (79 loc) · 1.8 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
package catalogue
import (
"strings"
"time"
"github.com/go-kit/kit/log"
)
// 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) List(tags []string, order string, pageNum, pageSize int) (socks []Sock, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "List",
"tags", strings.Join(tags, ", "),
"order", order,
"pageNum", pageNum,
"pageSize", pageSize,
"result", len(socks),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.List(tags, order, pageNum, pageSize)
}
func (mw loggingMiddleware) Count(tags []string) (n int, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Count",
"tags", strings.Join(tags, ", "),
"result", n,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Count(tags)
}
func (mw loggingMiddleware) Get(id string) (s Sock, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Get",
"id", id,
"sock", s.ID,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Get(id)
}
func (mw loggingMiddleware) Tags() (tags []string, err error) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Tags",
"result", len(tags),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Tags()
}
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()
}