-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.go
83 lines (71 loc) · 2.54 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
package main
import (
"fmt"
"time"
"github.com/go-kit/kit/log"
ec "github.com/mre/edgecast"
)
/*
* loggingMiddleware wraps a given EdgecastInterface and logs its functions.
* It logs information for the following keys:
* - method: the function that was called inside the given EdgecastInterface
* - output: the return data of that function
* - err: the returned error-value of that function
* - took: time in seconds that function needed from invocation to return
*/
type loggingMiddleware struct {
logger log.Logger
next EdgecastInterface
}
func (mw loggingMiddleware) Bandwidth(platform int) (bandwidthData *ec.BandwidthData, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log( // params: alternating key-value-key-value-...
"method", "Bandwidth",
"platform", fmt.Sprintf("%d(%s)", platform, Platforms[platform]),
"output", fmt.Sprintf("%+v", bandwidthData),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
bandwidthData, err = mw.next.Bandwidth(platform) // hand function call to service
return
}
func (mw loggingMiddleware) Connections(platform int) (connectionData *ec.ConnectionData, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log( // params: alternating key-value-key-value-...
"method", "Connections",
"platform", fmt.Sprintf("%d(%s)", platform, Platforms[platform]),
"output", fmt.Sprintf("%+v", connectionData),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
connectionData, err = mw.next.Connections(platform) // hand function call to service
return
}
func (mw loggingMiddleware) CacheStatus(platform int) (cacheStatusData *ec.CacheStatusData, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log( // params: alternating key-value-key-value-...
"method", "CacheStatus",
"platform", fmt.Sprintf("%d(%s)", platform, Platforms[platform]),
"output", fmt.Sprintf("%+v", cacheStatusData),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
cacheStatusData, err = mw.next.CacheStatus(platform) // hand function call to service
return
}
func (mw loggingMiddleware) StatusCodes(platform int) (statusCodeData *ec.StatusCodeData, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log( // params: alternating key-value-key-value-...
"method", "StatusCodes",
"platform", fmt.Sprintf("%d(%s)", platform, Platforms[platform]),
"output", fmt.Sprintf("%+v", statusCodeData),
"err", err,
"took", time.Since(begin),
)
}(time.Now())
statusCodeData, err = mw.next.StatusCodes(platform) // hand function call to service
return
}