diff --git a/internal/cache/manager.go b/internal/cache/manager.go index 59c8255..c43aba2 100644 --- a/internal/cache/manager.go +++ b/internal/cache/manager.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "proxy-go/internal/utils" "sort" "strings" "sync" @@ -198,7 +199,7 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca if existingItem != nil { 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 } @@ -222,7 +223,7 @@ func (cm *CacheManager) Put(key CacheKey, resp *http.Response, body []byte) (*Ca } 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 } @@ -278,7 +279,7 @@ func (cm *CacheManager) cleanup() { cacheItem := item.(*CacheItem) os.Remove(cacheItem.FilePath) 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()) 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 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 } @@ -413,7 +414,7 @@ func (cm *CacheManager) cleanStaleFiles() error { // 如果文件不在缓存记录中,删除它 if !fileFound { 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.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 } diff --git a/internal/handler/auth.go b/internal/handler/auth.go index 410c484..3000052 100644 --- a/internal/handler/auth.go +++ b/internal/handler/auth.go @@ -6,6 +6,7 @@ import ( "encoding/json" "log" "net/http" + "proxy-go/internal/utils" "strings" "sync" "time" @@ -74,6 +75,7 @@ func (h *ProxyHandler) CheckAuth(token string) bool { func (h *ProxyHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) { auth := r.Header.Get("Authorization") 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) return } @@ -81,6 +83,8 @@ func (h *ProxyHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) { token := strings.TrimPrefix(auth, "Bearer ") 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") json.NewEncoder(w).Encode(map[string]string{ "message": "已退出登录", @@ -92,12 +96,14 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { auth := r.Header.Get("Authorization") 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) return } token := strings.TrimPrefix(auth, "Bearer ") 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) return } @@ -109,29 +115,27 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc { // AuthHandler 处理认证请求 func (h *ProxyHandler) AuthHandler(w http.ResponseWriter, r *http.Request) { 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) return } // 解析表单数据 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) return } password := r.FormValue("password") - log.Printf("[Auth] 收到登录请求,密码长度: %d", len(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) return } 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) return } @@ -139,7 +143,7 @@ func (h *ProxyHandler) AuthHandler(w http.ResponseWriter, r *http.Request) { token := h.auth.generateToken() 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.WriteHeader(http.StatusOK) diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index a8804c7..4a78310 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -117,7 +117,7 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler { auth: newAuthManager(), Cache: cacheManager, 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.Write([]byte("Internal Server Error")) }, @@ -137,7 +137,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 添加 panic 恢复 defer func() { 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)) } }() @@ -157,8 +157,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { w.WriteHeader(http.StatusOK) fmt.Fprint(w, "Welcome to CZL proxy.") - log.Printf("[%s] %s %s -> %d (root path) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, http.StatusOK, time.Since(start)) + log.Printf("[Proxy] %s %s -> %d (%s) from %s", r.Method, r.URL.Path, http.StatusOK, utils.GetClientIP(r), utils.GetRequestSource(r)) return } @@ -176,8 +175,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 如果没有匹配的路径,返回 404 if matchedPrefix == "" { http.NotFound(w, r) - log.Printf("[%s] %s %s -> 404 (not found) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, time.Since(start)) + log.Printf("[Proxy] %s %s -> 404 (%s) from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) return } @@ -188,8 +186,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { decodedPath, err := url.QueryUnescape(targetPath) if err != nil { h.errorHandler(w, r, fmt.Errorf("error decoding path: %v", err)) - log.Printf("[%s] %s %s -> 500 (error decoding path: %v) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start)) + log.Printf("[Proxy] ERR %s %s -> 500 (%s) decode error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) return } @@ -208,8 +205,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { parsedURL, err := url.Parse(targetURL) if err != nil { h.errorHandler(w, r, fmt.Errorf("error parsing URL: %v", err)) - log.Printf("[%s] %s %s -> 500 (error parsing URL: %v) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start)) + log.Printf("[Proxy] ERR %s %s -> 500 (%s) parse error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) 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) if err != nil { 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 } @@ -293,12 +290,10 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err != nil { if ctx.Err() == context.DeadlineExceeded { h.errorHandler(w, r, fmt.Errorf("request timeout after %v", proxyRespTimeout)) - log.Printf("[Timeout] %s %s -> timeout after %v", - r.Method, r.URL.Path, proxyRespTimeout) + log.Printf("[Proxy] ERR %s %s -> 408 (%s) timeout from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) } else { h.errorHandler(w, r, fmt.Errorf("proxy error: %v", err)) - log.Printf("[%s] %s %s -> 502 (proxy error: %v) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(start)) + log.Printf("[Proxy] ERR %s %s -> 502 (%s) proxy error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) } return } @@ -325,14 +320,14 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } else { written, err = io.Copy(w, resp.Body) 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 } } } else { written, err = io.Copy(w, resp.Body) 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 } } diff --git a/internal/metrics/collector.go b/internal/metrics/collector.go index c3dd788..db4e166 100644 --- a/internal/metrics/collector.go +++ b/internal/metrics/collector.go @@ -323,13 +323,13 @@ func (c *Collector) SaveMetrics(stats map[string]interface{}) error { // LoadRecentStats 简化为只进行数据验证 func (c *Collector) LoadRecentStats() error { start := time.Now() - log.Printf("Starting to validate stats...") + log.Printf("[Metrics] Loading stats...") if err := c.validateLoadedData(); err != nil { 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 } diff --git a/internal/middleware/fixed_path_proxy.go b/internal/middleware/fixed_path_proxy.go index a17be66..3054041 100644 --- a/internal/middleware/fixed_path_proxy.go +++ b/internal/middleware/fixed_path_proxy.go @@ -68,8 +68,7 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body) if err != nil { http.Error(w, "Error creating proxy request", http.StatusInternalServerError) - log.Printf("[%s] %s %s -> 500 (error creating request: %v) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(startTime)) + log.Printf("[Fixed] ERR %s %s -> 500 (%s) create request error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) return } @@ -91,8 +90,7 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle resp, err := client.Do(proxyReq) if err != nil { http.Error(w, "Error forwarding request", http.StatusBadGateway) - log.Printf("[%s] %s %s -> 502 (proxy error: %v) [%v]", - utils.GetClientIP(r), r.Method, r.URL.Path, err, time.Since(startTime)) + log.Printf("[Fixed] ERR %s %s -> 502 (%s) proxy error from %s", r.Method, r.URL.Path, utils.GetClientIP(r), utils.GetRequestSource(r)) return } defer resp.Body.Close() @@ -126,8 +124,9 @@ func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handle written, err = io.Copy(w, resp.Body) } + // 写入响应错误处理 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)) } // 记录统计信息