forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
98 lines (78 loc) · 2.13 KB
/
log.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
package internal
import (
"bytes"
"fmt"
"net/http"
"sync"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
)
// LogContext is a context key.
var LogContext = contextKey("request_logs")
// A LogCollector is used to collect logs.
type LogCollector struct {
Messages []string
lock sync.Mutex
}
// Append appends msg into log context.
func (lc *LogCollector) Append(msg string) {
lc.lock.Lock()
lc.Messages = append(lc.Messages, msg)
lc.lock.Unlock()
}
// Flush flushes collected logs.
func (lc *LogCollector) Flush() string {
var buffer bytes.Buffer
start := true
for _, message := range lc.takeAll() {
if start {
start = false
} else {
buffer.WriteByte('\n')
}
buffer.WriteString(message)
}
return buffer.String()
}
func (lc *LogCollector) takeAll() []string {
lc.lock.Lock()
messages := lc.Messages
lc.Messages = nil
lc.lock.Unlock()
return messages
}
// Error logs the given v along with r in error log.
func Error(r *http.Request, v ...any) {
logx.WithContext(r.Context()).Error(format(r, v...))
}
// Errorf logs the given v with format along with r in error log.
func Errorf(r *http.Request, format string, v ...any) {
logx.WithContext(r.Context()).Error(formatf(r, format, v...))
}
// Info logs the given v along with r in access log.
func Info(r *http.Request, v ...any) {
appendLog(r, format(r, v...))
}
// Infof logs the given v with format along with r in access log.
func Infof(r *http.Request, format string, v ...any) {
appendLog(r, formatf(r, format, v...))
}
func appendLog(r *http.Request, message string) {
logs := r.Context().Value(LogContext)
if logs != nil {
logs.(*LogCollector).Append(message)
}
}
func format(r *http.Request, v ...any) string {
return formatWithReq(r, fmt.Sprint(v...))
}
func formatf(r *http.Request, format string, v ...any) string {
return formatWithReq(r, fmt.Sprintf(format, v...))
}
func formatWithReq(r *http.Request, v string) string {
return fmt.Sprintf("(%s - %s) %s", r.RequestURI, httpx.GetRemoteAddr(r), v)
}
type contextKey string
func (c contextKey) String() string {
return "rest/internal context key " + string(c)
}