feat(log): 全面重构日志系统,支持日志级别、去除ANSI乱码并抑制高频轮询
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"affiliate_dash/internal/handler"
|
||||
"affiliate_dash/internal/pkg/applog"
|
||||
"affiliate_dash/internal/pkg/jwt"
|
||||
"affiliate_dash/internal/pkg/openlog"
|
||||
"affiliate_dash/internal/service"
|
||||
"affiliate_dash/internal/testdb"
|
||||
|
||||
@@ -49,3 +55,96 @@ func TestSetupDoesNotPanic(t *testing.T) {
|
||||
OpenSignSkew: 300,
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsPollingEndpoint(t *testing.T) {
|
||||
tests := []struct {
|
||||
method string
|
||||
path string
|
||||
expected bool
|
||||
}{
|
||||
{"GET", "/health", true},
|
||||
{"GET", "/api/delivery/v1/orders/FO123/bind-result", true},
|
||||
{"GET", "/api/delivery/v1/orders/FO123", true},
|
||||
{"GET", "/api/client/v1/orders/FO123", true},
|
||||
{"GET", "/api/client/v1/wallet", true},
|
||||
{"GET", "/api/open/v1/orders/FO123", true},
|
||||
{"POST", "/api/client/v1/orders", false},
|
||||
{"POST", "/api/delivery/v1/orders/FO123/submit", false},
|
||||
{"POST", "/api/client/v1/orders/FO123/cancel", false},
|
||||
{"GET", "/api/dashboard", false},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
got := isPollingEndpoint(tc.method, tc.path)
|
||||
if got != tc.expected {
|
||||
t.Errorf("isPollingEndpoint(%s, %s) = %v, expected %v", tc.method, tc.path, got, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestLoggerGeneratesAndPassesReqID(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(requestLogger())
|
||||
r.GET("/test", func(c *gin.Context) {
|
||||
reqID := openlog.GetReqID(c)
|
||||
if reqID == "" || reqID == "-" {
|
||||
t.Errorf("expected req_id in context, got empty")
|
||||
}
|
||||
c.String(200, "ok")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
respReqID := w.Header().Get("X-Request-ID")
|
||||
if respReqID == "" {
|
||||
t.Errorf("expected X-Request-ID in response header")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestLoggerSuppressesPollingOnInfo(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
defer log.SetOutput(log.Writer())
|
||||
|
||||
applog.SetLevel("info")
|
||||
|
||||
r := gin.New()
|
||||
r.Use(requestLogger())
|
||||
r.GET("/health", func(c *gin.Context) {
|
||||
c.String(200, "ok")
|
||||
})
|
||||
r.GET("/api/client/v1/orders/:id", func(c *gin.Context) {
|
||||
c.String(200, "ok")
|
||||
})
|
||||
|
||||
// 1. In INFO level, polling requests with 200 should be silenced
|
||||
buf.Reset()
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
if buf.Len() > 0 {
|
||||
t.Errorf("expected /health 200 to be silenced in INFO mode, got: %s", buf.String())
|
||||
}
|
||||
|
||||
buf.Reset()
|
||||
req = httptest.NewRequest("GET", "/api/client/v1/orders/FO12345", nil)
|
||||
w = httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
if buf.Len() > 0 {
|
||||
t.Errorf("expected GET order 200 to be silenced in INFO mode, got: %s", buf.String())
|
||||
}
|
||||
|
||||
// 2. In DEBUG level, polling requests should appear as [DEBUG]
|
||||
applog.SetLevel("debug")
|
||||
buf.Reset()
|
||||
req = httptest.NewRequest("GET", "/api/client/v1/orders/FO12345", nil)
|
||||
w = httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
if !strings.Contains(buf.String(), "[DEBUG] [HTTP] 200 GET /api/client/v1/orders/FO12345") {
|
||||
t.Errorf("expected GET order to appear as [DEBUG] in DEBUG mode, got: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user