-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
64 lines (57 loc) · 1.87 KB
/
Copy pathlogging.go
File metadata and controls
64 lines (57 loc) · 1.87 KB
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
package wservice
import (
"time"
"github.com/go-kit/kit/log"
)
// The logging middleware amends the wallet service with a logger
// loggingMiddleware is the type of the wrapper around the core service and any other functionality layers
type loggingMiddleware struct {
logger log.Logger
next WalletService
}
// NewLogging is how the logging middleware (loggingMiddleware struct) is constructed (the function is exported so it can be used from outside the package)
func NewLogging(logger log.Logger, next WalletService) WalletService {
return &loggingMiddleware{
logger: logger,
next: next,
}
}
// GetTable function is implemented for logging layer as the request traverses through the logging layer down to the next layer
func (mw loggingMiddleware) GetTable(s string) (output []string, err error) {
// Log everything that the function sees in the provided format
defer func(begin time.Time) {
status := func(in []string) string {
if len(in) != 0 {
return "success"
}
return "no table"
}(output)
_ = mw.logger.Log(
"method", "getTables",
"input", s,
//"output", strings.Join(output, ","),
"output", status,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
// The function calls the next layer down
output, err = mw.next.GetTable(s)
return
}
// DoTransfer function is implemented for the logging layer as the request traverses through the logging layer down to the next layer
func (mw loggingMiddleware) DoTransfer(s string, t string, v string) (output string, err error) {
// Log everything that the function sees in the provided format
defer func(begin time.Time) {
_ = mw.logger.Log(
"method", "doTransfer",
"input", "From "+s+" to "+t+" amount "+v,
"output", output,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
// The function calls the next layer down
output, err = mw.next.DoTransfer(s, t, v)
return
}