service = $service; $this->credential = $credential; $this->requestId = $requestId; $this->method = $method; $this->url = $url; $this->headers = $headers; $this->params = $params; $this->response = $response; $this->statusCode = $statusCode; $this->responseTime = $responseTime; $this->success = $success; $this->errorMessage = $errorMessage; } /** * 获取事件数据数组 * * @return array */ public function toArray(): array { return [ 'service_id' => $this->service->id, 'service_code' => $this->service->code, 'credential_id' => $this->credential->id, 'request_id' => $this->requestId, 'method' => $this->method, 'url' => $this->url, 'headers' => $this->headers, 'params' => $this->params, 'response' => $this->response, 'status_code' => $this->statusCode, 'response_time' => $this->responseTime, 'success' => $this->success, 'error_message' => $this->errorMessage, 'timestamp' => now()->toISOString(), ]; } /** * 判断是否为错误调用 * * @return bool */ public function isError(): bool { return !$this->success || ($this->statusCode && $this->statusCode >= 400); } /** * 判断是否为慢调用 * * @param int $threshold 阈值(毫秒) * @return bool */ public function isSlowCall(int $threshold = 5000): bool { return $this->responseTime > $threshold; } /** * 获取日志级别 * * @return string */ public function getLogLevel(): string { if ($this->isError()) { return 'ERROR'; } if ($this->isSlowCall()) { return 'WARNING'; } return 'INFO'; } }