mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
refactor(logging): Standardize logging format across components
- Update logging in cache, auth, proxy, metrics, and fixed path middleware - Add consistent log message structure with method, path, status, and source - Improve error logging with more descriptive and uniform messages - Enhance log readability by using concise and informative log formats
This commit is contained in:
parent
1f277997d3
commit
4e3a17ecd4
15
internal/cache/manager.go
vendored
15
internal/cache/manager.go
vendored
@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"proxy-go/internal/utils"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -198,7 +199,7 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca
|
|||||||
|
|
||||||
if existingItem != nil {
|
if existingItem != nil {
|
||||||
cm.items.Store(key, existingItem)
|
cm.items.Store(key, existingItem)
|
||||||
log.Printf("[Cache] Reusing existing cache for %s", key.URL)
|
log.Printf("[Cache] HIT %s %s (%s) from %s", resp.Request.Method, key.URL, formatBytes(existingItem.Size), utils.GetRequestSource(resp.Request))
|
||||||
return existingItem, nil
|
return existingItem, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +223,7 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca
|
|||||||
}
|
}
|
||||||
|
|
||||||
cm.items.Store(key, item)
|
cm.items.Store(key, item)
|
||||||
log.Printf("[Cache] Cached %s (%s)", key.URL, formatBytes(item.Size))
|
log.Printf("[Cache] NEW %s %s (%s) from %s", resp.Request.Method, key.URL, formatBytes(item.Size), utils.GetRequestSource(resp.Request))
|
||||||
return item, nil
|
return item, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +279,7 @@ func (cm *CacheManager) cleanup() {
|
|||||||
cacheItem := item.(*CacheItem)
|
cacheItem := item.(*CacheItem)
|
||||||
os.Remove(cacheItem.FilePath)
|
os.Remove(cacheItem.FilePath)
|
||||||
cm.items.Delete(key)
|
cm.items.Delete(key)
|
||||||
log.Printf("[Cache] Removed expired item: %s", key.URL)
|
log.Printf("[Cache] DEL %s (expired)", key.URL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -365,7 +366,7 @@ func (cm *CacheManager) ClearCache() error {
|
|||||||
}
|
}
|
||||||
filePath := filepath.Join(cm.cacheDir, entry.Name())
|
filePath := filepath.Join(cm.cacheDir, entry.Name())
|
||||||
if err := os.Remove(filePath); err != nil {
|
if err := os.Remove(filePath); err != nil {
|
||||||
log.Printf("[Cache] Failed to remove file %s: %v", filePath, err)
|
log.Printf("[Cache] ERR Failed to remove file: %s", entry.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -394,7 +395,7 @@ func (cm *CacheManager) cleanStaleFiles() error {
|
|||||||
// 清理临时文件
|
// 清理临时文件
|
||||||
if strings.HasPrefix(entry.Name(), "temp-") {
|
if strings.HasPrefix(entry.Name(), "temp-") {
|
||||||
if err := os.Remove(filePath); err != nil {
|
if err := os.Remove(filePath); err != nil {
|
||||||
log.Printf("[Cache] Failed to remove temp file %s: %v", filePath, err)
|
log.Printf("[Cache] ERR Failed to remove temp file: %s", entry.Name())
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -413,7 +414,7 @@ func (cm *CacheManager) cleanStaleFiles() error {
|
|||||||
// 如果文件不在缓存记录中,删除它
|
// 如果文件不在缓存记录中,删除它
|
||||||
if !fileFound {
|
if !fileFound {
|
||||||
if err := os.Remove(filePath); err != nil {
|
if err := os.Remove(filePath); err != nil {
|
||||||
log.Printf("[Cache] Failed to remove stale file %s: %v", filePath, err)
|
log.Printf("[Cache] ERR Failed to remove stale file: %s", entry.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -473,7 +474,7 @@ func (cm *CacheManager) Commit(key CacheKey, tempPath string, resp *http.Respons
|
|||||||
|
|
||||||
cm.items.Store(key, item)
|
cm.items.Store(key, item)
|
||||||
cm.bytesSaved.Add(size)
|
cm.bytesSaved.Add(size)
|
||||||
log.Printf("[Cache] Cached %s (%s)", key.URL, formatBytes(size))
|
log.Printf("[Cache] NEW %s %s (%s)", resp.Request.Method, key.URL, formatBytes(size))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"proxy-go/internal/utils"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -74,6 +75,7 @@ func (h *ProxyHandler) CheckAuth(token string) bool {
|
|||||||
func (h *ProxyHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *ProxyHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
auth := r.Header.Get("Authorization")
|
auth := r.Header.Get("Authorization")
|
||||||
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
|
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
|
||||||
|
log.Printf("[Auth] ERR %s %s -> 401 (%s) no token from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -81,6 +83,8 @@ func (h *ProxyHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
token := strings.TrimPrefix(auth, "Bearer ")
|
token := strings.TrimPrefix(auth, "Bearer ")
|
||||||
h.auth.tokens.Delete(token)
|
h.auth.tokens.Delete(token)
|
||||||
|
|
||||||
|
log.Printf("[Auth] %s %s -> 200 (%s) logout success from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
"message": "已退出登录",
|
"message": "已退出登录",
|
||||||
@ -92,12 +96,14 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
auth := r.Header.Get("Authorization")
|
auth := r.Header.Get("Authorization")
|
||||||
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
|
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
|
||||||
|
log.Printf("[Auth] ERR %s %s -> 401 (%s) no token from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := strings.TrimPrefix(auth, "Bearer ")
|
token := strings.TrimPrefix(auth, "Bearer ")
|
||||||
if !h.auth.validateToken(token) {
|
if !h.auth.validateToken(token) {
|
||||||
|
log.Printf("[Auth] ERR %s %s -> 401 (%s) invalid token from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
http.Error(w, "Invalid token", http.StatusUnauthorized)
|
http.Error(w, "Invalid token", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -109,29 +115,27 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
// AuthHandler 处理认证请求
|
// AuthHandler 处理认证请求
|
||||||
func (h *ProxyHandler) AuthHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *ProxyHandler) AuthHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
log.Printf("[Auth] 方法不允许: %s", r.Method)
|
log.Printf("[Auth] ERR %s %s -> 405 (%s) method not allowed", r.Method, r.URL.Path, utils.GetClientIP(r))
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析表单数据
|
// 解析表单数据
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
log.Printf("[Auth] 表单解析失败: %v", err)
|
log.Printf("[Auth] ERR %s %s -> 400 (%s) form parse error", r.Method, r.URL.Path, utils.GetClientIP(r))
|
||||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
log.Printf("[Auth] 收到登录请求,密码长度: %d", len(password))
|
|
||||||
|
|
||||||
if password == "" {
|
if password == "" {
|
||||||
log.Printf("[Auth] 密码为空")
|
log.Printf("[Auth] ERR %s %s -> 400 (%s) empty password", r.Method, r.URL.Path, utils.GetClientIP(r))
|
||||||
http.Error(w, "Password is required", http.StatusBadRequest)
|
http.Error(w, "Password is required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if password != h.config.Metrics.Password {
|
if password != h.config.Metrics.Password {
|
||||||
log.Printf("[Auth] 密码错误")
|
log.Printf("[Auth] ERR %s %s -> 401 (%s) invalid password", r.Method, r.URL.Path, utils.GetClientIP(r))
|
||||||
http.Error(w, "Invalid password", http.StatusUnauthorized)
|
http.Error(w, "Invalid password", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -139,7 +143,7 @@ func (h *ProxyHandler) AuthHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
token := h.auth.generateToken()
|
token := h.auth.generateToken()
|
||||||
h.auth.addToken(token, time.Duration(h.config.Metrics.TokenExpiry)*time.Second)
|
h.auth.addToken(token, time.Duration(h.config.Metrics.TokenExpiry)*time.Second)
|
||||||
|
|
||||||
log.Printf("[Auth] 登录成功,生成令牌")
|
log.Printf("[Auth] %s %s -> 200 (%s) login success", r.Method, r.URL.Path, utils.GetClientIP(r))
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -117,7 +117,7 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler {
|
|||||||
auth: newAuthManager(),
|
auth: newAuthManager(),
|
||||||
Cache: cacheManager,
|
Cache: cacheManager,
|
||||||
errorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
errorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
log.Printf("[Error] %s %s -> %v", r.Method, r.URL.Path, err)
|
log.Printf("[Error] %s %s -> %v from %s", r.Method, r.URL.Path, err, utils.GetRequestSource(r))
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte("Internal Server Error"))
|
w.Write([]byte("Internal Server Error"))
|
||||||
},
|
},
|
||||||
@ -137,7 +137,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 添加 panic 恢复
|
// 添加 panic 恢复
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
log.Printf("[Panic] %s %s -> %v", r.Method, r.URL.Path, err)
|
log.Printf("[Panic] %s %s -> %v from %s", r.Method, r.URL.Path, err, utils.GetRequestSource(r))
|
||||||
h.errorHandler(w, r, fmt.Errorf("panic: %v", err))
|
h.errorHandler(w, r, fmt.Errorf("panic: %v", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -157,8 +157,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprint(w, "Welcome to CZL proxy.")
|
fmt.Fprint(w, "Welcome to CZL proxy.")
|
||||||
log.Printf("[%s] %s %s -> %d (root path) [%v]",
|
log.Printf("[Proxy] %s %s -> %d (%s) from %s", r.Method, r.URL.Path, http.StatusOK, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, http.StatusOK, time.Since(start))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,8 +175,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 如果没有匹配的路径,返回 404
|
// 如果没有匹配的路径,返回 404
|
||||||
if matchedPrefix == "" {
|
if matchedPrefix == "" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
log.Printf("[%s] %s %s -> 404 (not found) [%v]",
|
log.Printf("[Proxy] %s %s -> 404 (%s) from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, time.Since(start))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,8 +186,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
decodedPath, err := url.QueryUnescape(targetPath)
|
decodedPath, err := url.QueryUnescape(targetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.errorHandler(w, r, fmt.Errorf("error decoding path: %v", err))
|
h.errorHandler(w, r, fmt.Errorf("error decoding path: %v", err))
|
||||||
log.Printf("[%s] %s %s -> 500 (error decoding path: %v) [%v]",
|
log.Printf("[Proxy] ERR %s %s -> 500 (%s) decode error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,8 +205,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
parsedURL, err := url.Parse(targetURL)
|
parsedURL, err := url.Parse(targetURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.errorHandler(w, r, fmt.Errorf("error parsing URL: %v", err))
|
h.errorHandler(w, r, fmt.Errorf("error parsing URL: %v", err))
|
||||||
log.Printf("[%s] %s %s -> 500 (error parsing URL: %v) [%v]",
|
log.Printf("[Proxy] ERR %s %s -> 500 (%s) parse error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,6 +213,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
proxyReq, err := http.NewRequestWithContext(ctx, r.Method, targetURL, r.Body)
|
proxyReq, err := http.NewRequestWithContext(ctx, r.Method, targetURL, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.errorHandler(w, r, fmt.Errorf("error creating request: %v", err))
|
h.errorHandler(w, r, fmt.Errorf("error creating request: %v", err))
|
||||||
|
log.Printf("[Proxy] ERR %s %s -> 500 (%s) create request error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,12 +290,10 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if ctx.Err() == context.DeadlineExceeded {
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
h.errorHandler(w, r, fmt.Errorf("request timeout after %v", proxyRespTimeout))
|
h.errorHandler(w, r, fmt.Errorf("request timeout after %v", proxyRespTimeout))
|
||||||
log.Printf("[Timeout] %s %s -> timeout after %v",
|
log.Printf("[Proxy] ERR %s %s -> 408 (%s) timeout from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
r.Method, r.URL.Path, proxyRespTimeout)
|
|
||||||
} else {
|
} else {
|
||||||
h.errorHandler(w, r, fmt.Errorf("proxy error: %v", err))
|
h.errorHandler(w, r, fmt.Errorf("proxy error: %v", err))
|
||||||
log.Printf("[%s] %s %s -> 502 (proxy error: %v) [%v]",
|
log.Printf("[Proxy] ERR %s %s -> 502 (%s) proxy error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start))
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -325,14 +320,14 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
written, err = io.Copy(w, resp.Body)
|
written, err = io.Copy(w, resp.Body)
|
||||||
if err != nil && !isConnectionClosed(err) {
|
if err != nil && !isConnectionClosed(err) {
|
||||||
log.Printf("[%s] Error writing response: %v", utils.GetClientIP(r), err)
|
log.Printf("[Proxy] ERR %s %s -> write error (%s) from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
written, err = io.Copy(w, resp.Body)
|
written, err = io.Copy(w, resp.Body)
|
||||||
if err != nil && !isConnectionClosed(err) {
|
if err != nil && !isConnectionClosed(err) {
|
||||||
log.Printf("[%s] Error writing response: %v", utils.GetClientIP(r), err)
|
log.Printf("[Proxy] ERR %s %s -> write error (%s) from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -323,13 +323,13 @@ func (c *Collector) SaveMetrics(stats map[string]interface{}) error {
|
|||||||
// LoadRecentStats 简化为只进行数据验证
|
// LoadRecentStats 简化为只进行数据验证
|
||||||
func (c *Collector) LoadRecentStats() error {
|
func (c *Collector) LoadRecentStats() error {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
log.Printf("Starting to validate stats...")
|
log.Printf("[Metrics] Loading stats...")
|
||||||
|
|
||||||
if err := c.validateLoadedData(); err != nil {
|
if err := c.validateLoadedData(); err != nil {
|
||||||
return fmt.Errorf("data validation failed: %v", err)
|
return fmt.Errorf("data validation failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Successfully validated stats in %v", time.Since(start))
|
log.Printf("[Metrics] Loaded stats in %v", time.Since(start))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,8 +68,7 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle
|
|||||||
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
|
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
|
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
|
||||||
log.Printf("[%s] %s %s -> 500 (error creating request: %v) [%v]",
|
log.Printf("[Fixed] ERR %s %s -> 500 (%s) create request error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(startTime))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,8 +90,7 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle
|
|||||||
resp, err := client.Do(proxyReq)
|
resp, err := client.Do(proxyReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error forwarding request", http.StatusBadGateway)
|
http.Error(w, "Error forwarding request", http.StatusBadGateway)
|
||||||
log.Printf("[%s] %s %s -> 502 (proxy error: %v) [%v]",
|
log.Printf("[Fixed] ERR %s %s -> 502 (%s) proxy error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(startTime))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@ -126,8 +124,9 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle
|
|||||||
written, err = io.Copy(w, resp.Body)
|
written, err = io.Copy(w, resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 写入响应错误处理
|
||||||
if err != nil && !isConnectionClosed(err) {
|
if err != nil && !isConnectionClosed(err) {
|
||||||
log.Printf("[%s] Error writing response: %v", utils.GetClientIP(r), err)
|
log.Printf("[Fixed] ERR %s %s -> write error (%s) from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录统计信息
|
// 记录统计信息
|
||||||
|
Loading…
x
Reference in New Issue
Block a user