/
logging.go
113 lines (103 loc) · 3.04 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @Time : 2019-07-04 16:12
* @Author : ygqbasic@gmail.com
* @File : logging
* @Software: VS Code
*/
package pod
import (
"context"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/fuhsicloud/fuhsicloud/src/middleware"
"io"
"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) Detail(ctx context.Context, podName string) (res map[string]interface{}, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "Detail",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"podName", podName,
"err", err,
)
}(time.Now())
return s.Service.Detail(ctx, podName)
}
func (s *loggingService) ProjectPods(ctx context.Context) (res []map[string]interface{}, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "ProjectPods",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"err", err,
)
}(time.Now())
return s.Service.ProjectPods(ctx)
}
func (s *loggingService) GetLog(ctx context.Context, podName, container string, previous bool) (res *LogDetails, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "GetLog",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"podName", podName,
"container", container,
"previous", previous,
"err", err,
)
}(time.Now())
return s.Service.GetLog(ctx, podName, container, previous)
}
func (s *loggingService) DownloadLog(ctx context.Context, podName, container string, previous bool) (res io.ReadCloser, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "DownloadLog",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"podName", podName,
"container", container,
"previous", previous,
"err", err,
)
}(time.Now())
return s.Service.DownloadLog(ctx, podName, container, previous)
}
func (s *loggingService) Delete(ctx context.Context, podName string) (err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "Delete",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"podName", podName,
"err", err,
)
}(time.Now())
return s.Service.Delete(ctx, podName)
}
func (s *loggingService) PodsMetrics(ctx context.Context) (res map[string]interface{}, err error) {
defer func(begin time.Time) {
_ = s.logger.Log(
"uri", ctx.Value(kithttp.ContextKeyRequestURI),
"method", "Metrics",
"took", time.Since(begin),
"namespace", ctx.Value(middleware.NamespaceContext),
"err", err,
)
}(time.Now())
return s.Service.PodsMetrics(ctx)
}