diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index 6bc6d34..04ac34f 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -155,7 +155,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { for _, subKey := range subKeys { err := storage.Delete(subKey) if err != nil { - remotelogs.Error("HTTP_REQUEST_CACHE", "purge failed: "+err.Error()) + remotelogs.ErrorServer("HTTP_REQUEST_CACHE", "purge failed: "+err.Error()) } } @@ -260,7 +260,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { } if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: open cache failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: open cache failed: "+err.Error()) } return } @@ -320,7 +320,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { }) if err != nil { if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: read header failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: read header failed: "+err.Error()) } return } @@ -471,7 +471,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { return true } if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: "+err.Error()) } return } @@ -517,7 +517,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { }) if err != nil { if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: "+err.Error()) } return true } @@ -554,7 +554,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { this.varMapping["cache.status"] = "MISS" if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: read body failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_CACHE", this.URL()+": read from cache failed: read body failed: "+err.Error()) } return } diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index d018df0..85b4c1c 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -116,7 +116,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId // 处理Scheme if origin.Addr == nil { err := errors.New(this.URL() + ": Origin '" + strconv.FormatInt(origin.Id, 10) + "' does not has a address") - remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error()) + remotelogs.ErrorServer("HTTP_REQUEST_REVERSE_PROXY", err.Error()) this.write50x(err, http.StatusBadGateway, "Origin site did not has a valid address", "源站尚未配置地址", true) return } @@ -168,7 +168,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId var originHostIndex = strings.Index(originAddr, ":") if originHostIndex < 0 { var originErr = errors.New(this.URL() + ": Invalid origin address '" + originAddr + "', lacking port") - remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", originErr.Error()) + remotelogs.ErrorServer("HTTP_REQUEST_REVERSE_PROXY", originErr.Error()) this.write50x(originErr, http.StatusBadGateway, "No port in origin site address", "源站地址中没有配置端口", true) return } @@ -247,7 +247,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId // 获取请求客户端 client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects) if err != nil { - remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Create client failed: "+err.Error()) + remotelogs.ErrorServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Create client failed: "+err.Error()) this.write50x(err, http.StatusBadGateway, "Failed to create origin site client", "构造源站客户端失败", true) return } @@ -262,7 +262,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId this.reverseProxy.ResetScheduling() }) this.write50x(err, http.StatusBadGateway, "Failed to read origin site", "源站读取失败", true) - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+": Request origin server failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+": Request origin server failed: "+err.Error()) } else if httpErr.Err != context.Canceled { SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() { this.reverseProxy.ResetScheduling() @@ -278,7 +278,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId } if httpErr.Err != io.EOF { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+err.Error()) } return @@ -292,7 +292,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId this.write50x(err, http.StatusBadGateway, "Failed to read origin site", "源站读取失败", true) } if httpErr.Err != io.EOF { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+err.Error()) } } else { // 是否为客户端方面的错误 @@ -336,7 +336,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId if this.doWAFResponse(resp) { err = resp.Body.Close() if err != nil { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing Error (WAF): "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing Error (WAF): "+err.Error()) } return } @@ -346,7 +346,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId if len(this.web.Pages) > 0 && this.doPage(resp.StatusCode) { err = resp.Body.Close() if err != nil { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error (Page): "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error (Page): "+err.Error()) } return } @@ -448,13 +448,13 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId var closeErr = resp.Body.Close() if closeErr != nil { if !this.canIgnore(closeErr) { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error: "+closeErr.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error: "+closeErr.Error()) } } if err != nil && err != io.EOF { if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Writing error: "+err.Error()) + remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Writing error: "+err.Error()) this.addError(err) } } diff --git a/internal/remotelogs/utils.go b/internal/remotelogs/utils.go index 113b07d..32abc52 100644 --- a/internal/remotelogs/utils.go +++ b/internal/remotelogs/utils.go @@ -37,6 +37,11 @@ func init() { }) } +// Debug 打印调试信息 +func Debug(tag string, description string) { + logs.Println("[" + tag + "]" + description) +} + // Println 打印普通信息 func Println(tag string, description string) { logs.Println("[" + tag + "]" + description) @@ -73,6 +78,31 @@ func Warn(tag string, description string) { } } +// WarnServer 打印服务相关警告 +func WarnServer(tag string, description string) { + if Tea.IsTesting() { + logs.Println("[" + tag + "]" + description) + } + + nodeConfig, _ := nodeconfigs.SharedNodeConfig() + if nodeConfig != nil && nodeConfig.GlobalServerConfig != nil && !nodeConfig.GlobalServerConfig.Log.RecordServerError { + return + } + + select { + case logChan <- &pb.NodeLog{ + Role: teaconst.Role, + Tag: tag, + Description: description, + Level: "warning", + NodeId: teaconst.NodeId, + CreatedAt: time.Now().Unix(), + }: + default: + + } +} + // Error 打印错误信息 func Error(tag string, description string) { logs.Println("[" + tag + "]" + description) @@ -97,6 +127,37 @@ func Error(tag string, description string) { } } +// ErrorServer 打印服务相关错误信息 +func ErrorServer(tag string, description string) { + if Tea.IsTesting() { + logs.Println("[" + tag + "]" + description) + } + + // 忽略RPC连接错误 + var level = "error" + if strings.Contains(description, "code = Unavailable desc") { + level = "warning" + } + + nodeConfig, _ := nodeconfigs.SharedNodeConfig() + if nodeConfig != nil && nodeConfig.GlobalServerConfig != nil && !nodeConfig.GlobalServerConfig.Log.RecordServerError { + return + } + + select { + case logChan <- &pb.NodeLog{ + Role: teaconst.Role, + Tag: tag, + Description: description, + Level: level, + NodeId: teaconst.NodeId, + CreatedAt: time.Now().Unix(), + }: + default: + + } +} + // ErrorObject 打印错误对象 func ErrorObject(tag string, err error) { if err == nil { @@ -111,14 +172,16 @@ func ErrorObject(tag string, err error) { // ServerError 打印服务相关错误信息 func ServerError(serverId int64, tag string, description string, logType nodeconfigs.NodeLogType, params maps.Map) { + if Tea.IsTesting() { + logs.Println("[" + tag + "]" + description) + } + // 是否记录服务相关错误 nodeConfig, _ := nodeconfigs.SharedNodeConfig() if nodeConfig != nil && nodeConfig.GlobalServerConfig != nil && !nodeConfig.GlobalServerConfig.Log.RecordServerError { return } - logs.Println("[" + tag + "]" + description) - // 参数 var paramsJSON []byte if len(params) > 0 {