-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogging.go
65 lines (55 loc) · 1.27 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
package main
import (
"context"
"time"
"github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/log"
)
type loggingMiddleware struct {
logger log.Logger
next StringService
}
func (mw loggingMiddleware) Uppercase(ctx context.Context, s string) (output string, err error) {
defer func(begin time.Time) {
custCl, _ := ctx.Value(jwt.JWTClaimsContextKey).(*customClaims)
_ = mw.logger.Log(
"method", "uppercase",
"client", custCl.ClientID,
"input", s,
"output", output,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
output, err = mw.next.Uppercase(ctx, s)
return
}
func (mw loggingMiddleware) Count(ctx context.Context, s string) (n int) {
defer func(begin time.Time) {
_ = mw.logger.Log(
"method", "count",
"input", s,
"n", n,
"took", time.Since(begin),
)
}(time.Now())
n = mw.next.Count(ctx, s)
return
}
type loggingAuthMiddleware struct {
logger log.Logger
next AuthService
}
func (mw loggingAuthMiddleware) Auth(clientID string, clientSecret string) (token string, err error) {
defer func(begin time.Time) {
_ = mw.logger.Log(
"method", "auth",
"clientID", clientID,
"token", token,
"err", err,
"took", time.Since(begin),
)
}(time.Now())
token, err = mw.next.Auth(clientID, clientSecret)
return
}