forked from yiigo/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
81 lines (69 loc) · 1.47 KB
/
logger.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
package internal
import (
"context"
"net/http"
"strconv"
"strings"
)
// ReqLog 请求日志
type ReqLog struct {
err error
data map[string]string
}
// Set 设置日志K-V
func (l *ReqLog) Set(k, v string) {
l.data[k] = v
}
func (l *ReqLog) SetError(err error) {
l.err = err
}
// SetReqHeader 设置请求头
func (l *ReqLog) SetReqHeader(h http.Header) {
l.data["request_header"] = HeaderEncode(h)
}
// SetBody 设置请求Body
func (l *ReqLog) SetReqBody(v string) {
l.data["request_body"] = v
}
// SetRespHeader 设置返回头
func (l *ReqLog) SetRespHeader(h http.Header) {
l.data["response_header"] = HeaderEncode(h)
}
// SetResp 设置返回报文
func (l *ReqLog) SetRespBody(v string) {
l.data["response_body"] = v
}
// SetStatusCode 设置HTTP状态码
func (l *ReqLog) SetStatusCode(code int) {
l.data["status_code"] = strconv.Itoa(code)
}
// Do 日志记录
func (l *ReqLog) Do(ctx context.Context, log func(ctx context.Context, err error, data map[string]string)) {
if log == nil {
return
}
log(ctx, l.err, l.data)
}
// NewReqLog 生成请求日志
func NewReqLog(method, reqURL string) *ReqLog {
return &ReqLog{
data: map[string]string{
"method": method,
"url": reqURL,
},
}
}
func HeaderEncode(h http.Header) string {
var buf strings.Builder
for k, vals := range h {
for _, v := range vals {
if buf.Len() > 0 {
buf.WriteString("&")
}
buf.WriteString(k)
buf.WriteString("=")
buf.WriteString(v)
}
}
return buf.String()
}