|
|
@@ -8,26 +8,28 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
|
/**
|
|
|
* 第三方服务调用日志模型
|
|
|
- *
|
|
|
- * @property int $id 主键ID
|
|
|
- * @property int $service_id 服务ID
|
|
|
- * @property int|null $credential_id 凭证ID
|
|
|
- * @property string $request_id 请求ID
|
|
|
- * @property string $method 请求方法
|
|
|
- * @property string $url 请求URL
|
|
|
- * @property array|null $headers 请求头
|
|
|
- * @property array|null $params 请求参数
|
|
|
- * @property string|null $body 请求体
|
|
|
- * @property int|null $response_status 响应状态码
|
|
|
- * @property array|null $response_headers 响应头
|
|
|
- * @property string|null $response_body 响应体
|
|
|
- * @property int|null $response_time 响应时间
|
|
|
- * @property string|null $error_message 错误信息
|
|
|
- * @property string $level 日志级别
|
|
|
- * @property int|null $user_id 用户ID
|
|
|
- * @property string|null $ip_address IP地址
|
|
|
- * @property string|null $user_agent User Agent
|
|
|
- * @property \Carbon\Carbon $created_at 创建时间
|
|
|
+ *
|
|
|
+ * field start
|
|
|
+ * @property int $id 主键ID
|
|
|
+ * @property int $service_id 服务ID
|
|
|
+ * @property int $credential_id 凭证ID
|
|
|
+ * @property string $request_id 请求ID(用于追踪)
|
|
|
+ * @property string $method 请求方法
|
|
|
+ * @property string $url 请求URL
|
|
|
+ * @property array $headers 请求头
|
|
|
+ * @property array $params 请求参数
|
|
|
+ * @property string $body 请求体
|
|
|
+ * @property int $response_status 响应状态码
|
|
|
+ * @property array $response_headers 响应头
|
|
|
+ * @property string $response_body 响应体
|
|
|
+ * @property int $response_time 响应时间(毫秒)
|
|
|
+ * @property string $error_message 错误信息
|
|
|
+ * @property string $level 日志级别
|
|
|
+ * @property int $user_id 用户ID
|
|
|
+ * @property string $ip_address IP地址
|
|
|
+ * @property string $user_agent User Agent
|
|
|
+ * @property \Carbon\Carbon $created_at 创建时间
|
|
|
+ * field end
|
|
|
*/
|
|
|
class ThirdPartyLog extends ModelCore
|
|
|
{
|
|
|
@@ -45,35 +47,7 @@ class ThirdPartyLog extends ModelCore
|
|
|
*/
|
|
|
public $timestamps = false;
|
|
|
|
|
|
- // field start
|
|
|
- /**
|
|
|
- * 可批量赋值的属性
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- // attrlist start
|
|
|
- protected $fillable = [
|
|
|
- 'service_id',
|
|
|
- 'credential_id',
|
|
|
- 'request_id',
|
|
|
- 'method',
|
|
|
- 'url',
|
|
|
- 'headers',
|
|
|
- 'params',
|
|
|
- 'body',
|
|
|
- 'response_status',
|
|
|
- 'response_headers',
|
|
|
- 'response_body',
|
|
|
- 'response_time',
|
|
|
- 'error_message',
|
|
|
- 'level',
|
|
|
- 'user_id',
|
|
|
- 'ip_address',
|
|
|
- 'user_agent',
|
|
|
- 'created_at',
|
|
|
- ];
|
|
|
- // attrlist end
|
|
|
- // field end
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 属性类型转换
|
|
|
@@ -162,11 +136,11 @@ class ThirdPartyLog extends ModelCore
|
|
|
if (!$this->response_time) {
|
|
|
return '-';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if ($this->response_time < 1000) {
|
|
|
return $this->response_time . 'ms';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return number_format($this->getResponseTimeInSeconds(), 2) . 's';
|
|
|
}
|
|
|
|
|
|
@@ -180,7 +154,7 @@ class ThirdPartyLog extends ModelCore
|
|
|
if (!$this->response_status) {
|
|
|
return '无响应';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
$statusTexts = [
|
|
|
200 => 'OK',
|
|
|
201 => 'Created',
|
|
|
@@ -195,7 +169,7 @@ class ThirdPartyLog extends ModelCore
|
|
|
503 => 'Service Unavailable',
|
|
|
504 => 'Gateway Timeout',
|
|
|
];
|
|
|
-
|
|
|
+
|
|
|
return $statusTexts[$this->response_status] ?? 'Unknown';
|
|
|
}
|
|
|
|
|
|
@@ -209,19 +183,19 @@ class ThirdPartyLog extends ModelCore
|
|
|
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';
|
|
|
}
|
|
|
|
|
|
@@ -236,11 +210,11 @@ class ThirdPartyLog extends ModelCore
|
|
|
if (!$this->body) {
|
|
|
return '';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (strlen($this->body) <= $maxLength) {
|
|
|
return $this->body;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return substr($this->body, 0, $maxLength) . '...';
|
|
|
}
|
|
|
|
|
|
@@ -255,11 +229,11 @@ class ThirdPartyLog extends ModelCore
|
|
|
if (!$this->response_body) {
|
|
|
return '';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (strlen($this->response_body) <= $maxLength) {
|
|
|
return $this->response_body;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return substr($this->response_body, 0, $maxLength) . '...';
|
|
|
}
|
|
|
|
|
|
@@ -322,12 +296,12 @@ class ThirdPartyLog extends ModelCore
|
|
|
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'])) {
|
|
|
@@ -342,7 +316,7 @@ class ThirdPartyLog extends ModelCore
|
|
|
$data['level'] = LOG_LEVEL::INFO->value;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return static::create($data);
|
|
|
}
|
|
|
|