'integer', 'credential_id' => 'integer', 'headers' => 'array', 'params' => 'array', 'response_status' => 'integer', 'response_headers' => 'array', 'response_time' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime', ]; /** * 获取日志级别枚举 * * @return LOG_LEVEL */ public function getLogLevelEnum(): LOG_LEVEL { return LOG_LEVEL::from($this->level); } /** * 获取日志级别标签 * * @return string */ public function getLevelLabel(): string { return $this->getLogLevelEnum()->getLabel(); } /** * 检查是否为成功请求 * * @return bool */ public function isSuccessful(): bool { return $this->response_status >= 200 && $this->response_status < 300; } /** * 检查是否为错误请求 * * @return bool */ public function isError(): bool { return $this->getLogLevelEnum()->isError() || $this->response_status >= 400; } /** * 检查是否需要告警 * * @return bool */ public function needsAlert(): bool { return $this->getLogLevelEnum()->needsAlert(); } /** * 获取响应时间(秒) * * @return float */ public function getResponseTimeInSeconds(): float { return $this->response_time ? $this->response_time / 1000 : 0; } /** * 获取格式化的响应时间 * * @return string */ public function getFormattedResponseTime(): string { if (!$this->response_time) { return '-'; } if ($this->response_time < 1000) { return $this->response_time . 'ms'; } return number_format($this->getResponseTimeInSeconds(), 2) . 's'; } /** * 获取状态码描述 * * @return string */ public function getStatusDescription(): string { if (!$this->response_status) { return '无响应'; } $statusTexts = [ 200 => 'OK', 201 => 'Created', 204 => 'No Content', 400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 429 => 'Too Many Requests', 500 => 'Internal Server Error', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', ]; return $statusTexts[$this->response_status] ?? 'Unknown'; } /** * 获取状态码颜色 * * @return string */ public function getStatusColor(): string { if (!$this->response_status) { return 'secondary'; } if ($this->response_status >= 200 && $this->response_status < 300) { return 'success'; } if ($this->response_status >= 300 && $this->response_status < 400) { return 'info'; } if ($this->response_status >= 400 && $this->response_status < 500) { return 'warning'; } return 'danger'; } /** * 获取简化的请求体 * * @param int $maxLength * @return string */ public function getTruncatedBody(int $maxLength = 200): string { if (!$this->body) { return ''; } if (strlen($this->body) <= $maxLength) { return $this->body; } return substr($this->body, 0, $maxLength) . '...'; } /** * 获取简化的响应体 * * @param int $maxLength * @return string */ public function getTruncatedResponseBody(int $maxLength = 200): string { if (!$this->response_body) { return ''; } if (strlen($this->response_body) <= $maxLength) { return $this->response_body; } return substr($this->response_body, 0, $maxLength) . '...'; } /** * 获取请求的基本信息 * * @return array */ public function getRequestSummary(): array { return [ 'method' => $this->method, 'url' => $this->url, 'status' => $this->response_status, 'response_time' => $this->getFormattedResponseTime(), 'level' => $this->getLevelLabel(), 'created_at' => $this->created_at->format('Y-m-d H:i:s'), ]; } /** * 关联第三方服务 * * @return BelongsTo */ public function service(): BelongsTo { return $this->belongsTo(ThirdPartyService::class, 'service_id'); } /** * 关联认证凭证 * * @return BelongsTo */ public function credential(): BelongsTo { return $this->belongsTo(ThirdPartyCredential::class, 'credential_id'); } /** * 生成唯一的请求ID * * @return string */ public static function generateRequestId(): string { return uniqid('req_', true); } /** * 创建日志记录 * * @param array $data * @return static */ public static function createLog(array $data): static { // 自动设置创建时间 if (!isset($data['created_at'])) { $data['created_at'] = now(); } // 自动生成请求ID if (!isset($data['request_id'])) { $data['request_id'] = static::generateRequestId(); } // 自动设置日志级别 if (!isset($data['level'])) { if (isset($data['response_status'])) { if ($data['response_status'] >= 400) { $data['level'] = LOG_LEVEL::ERROR->value; } elseif ($data['response_status'] >= 300) { $data['level'] = LOG_LEVEL::WARNING->value; } else { $data['level'] = LOG_LEVEL::INFO->value; } } else { $data['level'] = LOG_LEVEL::INFO->value; } } return static::create($data); } /** * 按日期范围查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $startDate * @param string $endDate * @return \Illuminate\Database\Eloquent\Builder */ public function scopeDateRange($query, string $startDate, string $endDate) { return $query->whereBetween('created_at', [$startDate, $endDate]); } /** * 按服务查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param int $serviceId * @return \Illuminate\Database\Eloquent\Builder */ public function scopeByService($query, int $serviceId) { return $query->where('service_id', $serviceId); } /** * 按日志级别查询 * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $level * @return \Illuminate\Database\Eloquent\Builder */ public function scopeByLevel($query, string $level) { return $query->where('level', $level); } /** * 查询错误日志 * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeErrors($query) { return $query->whereIn('level', [LOG_LEVEL::ERROR->value, LOG_LEVEL::CRITICAL->value]) ->orWhere('response_status', '>=', 400); } }