Files
sam-manage/app/Models/ApiRequestLog.php
hskwon f5e2068557 MNG: API 로그 관리 기능 추가
- ApiLogController: 로그 목록/상세/삭제 기능
- ApiRequestLog 모델: 색상 accessor, 경로 추출
- 뷰: 통계, 필터링, 페이지네이션
- 사이드바에 'API 요청 로그' 메뉴 추가
- JSON 출력 stripslashes 적용 (이스케이프 제거)
2025-12-15 15:51:19 +09:00

102 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* API 요청/응답 로그 모델
*
* @property int $id
* @property string $method
* @property string $url
* @property string|null $route_name
* @property array|null $request_headers
* @property array|null $request_body
* @property array|null $request_query
* @property int $response_status
* @property string|null $response_body
* @property int $duration_ms
* @property string|null $ip_address
* @property string|null $user_agent
* @property int|null $user_id
* @property int|null $tenant_id
* @property \Carbon\Carbon $created_at
*/
class ApiRequestLog extends Model
{
protected $table = 'api_request_logs';
public $timestamps = false;
protected $fillable = [
'method',
'url',
'route_name',
'request_headers',
'request_body',
'request_query',
'response_status',
'response_body',
'duration_ms',
'ip_address',
'user_agent',
'user_id',
'tenant_id',
];
protected $casts = [
'request_headers' => 'array',
'request_body' => 'array',
'request_query' => 'array',
'response_status' => 'integer',
'duration_ms' => 'integer',
'user_id' => 'integer',
'tenant_id' => 'integer',
'created_at' => 'datetime',
];
/**
* HTTP 메서드별 색상
*/
public function getMethodColorAttribute(): string
{
return match ($this->method) {
'GET' => 'bg-green-100 text-green-800',
'POST' => 'bg-blue-100 text-blue-800',
'PUT', 'PATCH' => 'bg-yellow-100 text-yellow-800',
'DELETE' => 'bg-red-100 text-red-800',
default => 'bg-gray-100 text-gray-800',
};
}
/**
* 상태 코드별 색상
*/
public function getStatusColorAttribute(): string
{
return match (true) {
$this->response_status >= 500 => 'bg-red-100 text-red-800',
$this->response_status >= 400 => 'bg-yellow-100 text-yellow-800',
$this->response_status >= 300 => 'bg-blue-100 text-blue-800',
$this->response_status >= 200 => 'bg-green-100 text-green-800',
default => 'bg-gray-100 text-gray-800',
};
}
/**
* URL에서 경로만 추출
*/
public function getPathAttribute(): string
{
return parse_url($this->url, PHP_URL_PATH) ?? $this->url;
}
/**
* 하루 지난 로그 삭제
*/
public static function pruneOldLogs(): int
{
return static::where('created_at', '<', now()->subDay())->delete();
}
}