/
api_slog_record.go
125 lines (120 loc) · 4.85 KB
/
api_slog_record.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
114
115
116
117
118
119
120
121
122
123
124
125
package golog
import (
"context"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/gorequest"
"github.com/dtapps/go-library/utils/gotrace_id"
"github.com/dtapps/go-library/utils/gourl"
"time"
)
// 结构体
type apiSLog struct {
TraceID string `json:"trace_id,omitempty"`
RequestTime time.Time `json:"request_time,omitempty"`
RequestUri string `json:"request_uri,omitempty"`
RequestUrl string `json:"request_url,omitempty"`
RequestApi string `json:"request_api,omitempty"`
RequestMethod string `json:"request_method,omitempty"`
RequestParams map[string]interface{} `json:"request_params,omitempty"`
RequestHeader map[string]string `json:"request_header,omitempty"`
ResponseHeader map[string][]string `json:"response_header,omitempty"`
ResponseStatusCode int `json:"response_status_code,omitempty"`
ResponseBody map[string]interface{} `json:"response_body,omitempty"`
ResponseTime time.Time `json:"response_time,omitempty,omitempty"`
}
// Middleware 中间件
func (sl *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) {
data := apiSLog{
TraceID: gotrace_id.GetTraceIdContext(ctx),
RequestTime: request.RequestTime,
RequestUri: request.RequestUri,
RequestUrl: gourl.UriParse(request.RequestUri).Url,
RequestApi: gourl.UriParse(request.RequestUri).Path,
RequestMethod: request.RequestMethod,
RequestParams: request.RequestParams,
RequestHeader: request.RequestHeader,
ResponseHeader: request.ResponseHeader,
ResponseStatusCode: request.ResponseStatusCode,
ResponseBody: dorm.JsonDecodeNoError(request.ResponseBody),
ResponseTime: request.ResponseTime,
}
if sl.slog.status {
sl.slog.client.WithTraceId(ctx).Info("Middleware",
"request_time", data.RequestTime,
"request_uri", data.RequestUri,
"request_url", data.RequestUrl,
"request_api", data.RequestApi,
"request_method", data.RequestMethod,
"request_params", data.RequestParams,
"request_header", data.RequestHeader,
"response_header", data.ResponseHeader,
"response_status_code", data.ResponseStatusCode,
"response_body", data.ResponseBody,
"response_time", data.ResponseTime,
)
}
}
// MiddlewareXml 中间件
func (sl *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response) {
data := apiSLog{
TraceID: gotrace_id.GetTraceIdContext(ctx),
RequestTime: request.RequestTime,
RequestUri: request.RequestUri,
RequestUrl: gourl.UriParse(request.RequestUri).Url,
RequestApi: gourl.UriParse(request.RequestUri).Path,
RequestMethod: request.RequestMethod,
RequestParams: request.RequestParams,
RequestHeader: request.RequestHeader,
ResponseHeader: request.ResponseHeader,
ResponseStatusCode: request.ResponseStatusCode,
ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody),
ResponseTime: request.ResponseTime,
}
if sl.slog.status {
sl.slog.client.WithTraceId(ctx).Info("MiddlewareXml",
"request_time", data.RequestTime,
"request_uri", data.RequestUri,
"request_url", data.RequestUrl,
"request_api", data.RequestApi,
"request_method", data.RequestMethod,
"request_params", data.RequestParams,
"request_header", data.RequestHeader,
"response_header", data.ResponseHeader,
"response_status_code", data.ResponseStatusCode,
"response_body", data.ResponseBody,
"response_time", data.ResponseTime,
)
}
}
// MiddlewareCustom 中间件
func (sl *ApiSLog) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
data := apiSLog{
TraceID: gotrace_id.GetTraceIdContext(ctx),
RequestTime: request.RequestTime,
RequestUri: request.RequestUri,
RequestUrl: gourl.UriParse(request.RequestUri).Url,
RequestApi: api,
RequestMethod: request.RequestMethod,
RequestParams: request.RequestParams,
RequestHeader: request.RequestHeader,
ResponseHeader: request.ResponseHeader,
ResponseStatusCode: request.ResponseStatusCode,
ResponseBody: dorm.JsonDecodeNoError(request.ResponseBody),
ResponseTime: request.ResponseTime,
}
if sl.slog.status {
sl.slog.client.WithTraceId(ctx).Info("MiddlewareCustom",
"request_time", data.RequestTime,
"request_uri", data.RequestUri,
"request_url", data.RequestUrl,
"request_api", data.RequestApi,
"request_method", data.RequestMethod,
"request_params", data.RequestParams,
"request_header", data.RequestHeader,
"response_header", data.ResponseHeader,
"response_status_code", data.ResponseStatusCode,
"response_body", data.ResponseBody,
"response_time", data.ResponseTime,
)
}
}