/
logging.go
48 lines (42 loc) · 1.05 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
/**
* @Time : 2019-07-09 18:50
* @Author : soupzhb@gmail.com
* @File : endpoint.go
* @Software: VS Code
*/
package wechat
import (
"context"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kithttp "github.com/go-kit/kit/transport/http"
"time"
)
type loggingService struct {
logger log.Logger
Service
}
func NewLoggingService(logger log.Logger, s Service) Service {
return &loggingService{level.Info(logger), s}
}
func (s loggingService) Receive(ctx context.Context) (str, contentType string, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"path", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "get",
"took", time.Since(begin),
)
}(time.Now())
return s.Service.Receive(ctx)
}
func (s loggingService) GetQr(ctx context.Context, req qrRequest) (res interface{}, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"path", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "post",
"email", req.Email,
"took", time.Since(begin),
)
}(time.Now())
return s.Service.GetQr(ctx, req)
}