initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

84
core/httplog/log.go Normal file
View File

@@ -0,0 +1,84 @@
package httplog
import (
"bytes"
"fmt"
"net/http"
"sync"
"zero/core/httpx"
"zero/core/logx"
)
const LogContext = "request_logs"
type LogCollector struct {
Messages []string
lock sync.Mutex
}
func (lc *LogCollector) Append(msg string) {
lc.lock.Lock()
lc.Messages = append(lc.Messages, msg)
lc.lock.Unlock()
}
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
}
func Error(r *http.Request, v ...interface{}) {
logx.ErrorCaller(1, format(r, v...))
}
func Errorf(r *http.Request, format string, v ...interface{}) {
logx.ErrorCaller(1, formatf(r, format, v...))
}
func Info(r *http.Request, v ...interface{}) {
appendLog(r, format(r, v...))
}
func Infof(r *http.Request, format string, v ...interface{}) {
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 ...interface{}) string {
return formatWithReq(r, fmt.Sprint(v...))
}
func formatf(r *http.Request, format string, v ...interface{}) 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)
}

38
core/httplog/log_test.go Normal file
View File

@@ -0,0 +1,38 @@
package httplog
import (
"context"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInfo(t *testing.T) {
collector := new(LogCollector)
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req = req.WithContext(context.WithValue(req.Context(), LogContext, collector))
Info(req, "first")
Infof(req, "second %s", "third")
val := collector.Flush()
assert.True(t, strings.Contains(val, "first"))
assert.True(t, strings.Contains(val, "second"))
assert.True(t, strings.Contains(val, "third"))
assert.True(t, strings.Contains(val, "\n"))
}
func TestError(t *testing.T) {
var writer strings.Builder
log.SetOutput(&writer)
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
Error(req, "first")
Errorf(req, "second %s", "third")
val := writer.String()
assert.True(t, strings.Contains(val, "first"))
assert.True(t, strings.Contains(val, "second"))
assert.True(t, strings.Contains(val, "third"))
assert.True(t, strings.Contains(val, "\n"))
}