You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rest/internal/log.go

99 lines
2.1 KiB
Go

4 years ago
package internal
4 years ago
import (
"bytes"
"fmt"
"net/http"
"sync"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
4 years ago
)
// LogContext is a context key.
var LogContext = contextKey("request_logs")
4 years ago
// A LogCollector is used to collect logs.
4 years ago
type LogCollector struct {
Messages []string
lock sync.Mutex
}
// Append appends msg into log context.
4 years ago
func (lc *LogCollector) Append(msg string) {
lc.lock.Lock()
lc.Messages = append(lc.Messages, msg)
lc.lock.Unlock()
}
// Flush flushes collected logs.
4 years ago
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...))
4 years ago
}
// 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...))
4 years ago
}
// Info logs the given v along with r in access log.
func Info(r *http.Request, v ...any) {
4 years ago
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) {
4 years ago
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 {
4 years ago
return formatWithReq(r, fmt.Sprint(v...))
}
func formatf(r *http.Request, format string, v ...any) string {
4 years ago
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)
4 years ago
}
type contextKey string
func (c contextKey) String() string {
return "rest/internal context key " + string(c)
}