- ApiLogController: 로그 목록/상세/삭제 기능 - ApiRequestLog 모델: 색상 accessor, 경로 추출 - 뷰: 통계, 필터링, 페이지네이션 - 사이드바에 'API 요청 로그' 메뉴 추가 - JSON 출력 stripslashes 적용 (이스케이프 제거)
74 lines
2.1 KiB
PHP
74 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ApiRequestLog;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ApiLogController extends Controller
|
|
{
|
|
/**
|
|
* API 로그 목록 화면
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$query = ApiRequestLog::query()->orderByDesc('created_at');
|
|
|
|
// 필터: HTTP 메서드
|
|
if ($request->filled('method')) {
|
|
$query->where('method', $request->method);
|
|
}
|
|
|
|
// 필터: 상태 코드
|
|
if ($request->filled('status')) {
|
|
$status = $request->status;
|
|
if ($status === '2xx') {
|
|
$query->whereBetween('response_status', [200, 299]);
|
|
} elseif ($status === '4xx') {
|
|
$query->whereBetween('response_status', [400, 499]);
|
|
} elseif ($status === '5xx') {
|
|
$query->whereBetween('response_status', [500, 599]);
|
|
}
|
|
}
|
|
|
|
// 필터: URL 검색
|
|
if ($request->filled('search')) {
|
|
$query->where('url', 'like', '%' . $request->search . '%');
|
|
}
|
|
|
|
// 통계
|
|
$stats = [
|
|
'total' => ApiRequestLog::count(),
|
|
'success' => ApiRequestLog::whereBetween('response_status', [200, 299])->count(),
|
|
'client_error' => ApiRequestLog::whereBetween('response_status', [400, 499])->count(),
|
|
'server_error' => ApiRequestLog::whereBetween('response_status', [500, 599])->count(),
|
|
'avg_duration' => (int) ApiRequestLog::avg('duration_ms'),
|
|
];
|
|
|
|
$logs = $query->paginate(50)->withQueryString();
|
|
|
|
return view('api-logs.index', compact('logs', 'stats'));
|
|
}
|
|
|
|
/**
|
|
* API 로그 상세 화면
|
|
*/
|
|
public function show(int $id): View
|
|
{
|
|
$log = ApiRequestLog::findOrFail($id);
|
|
|
|
return view('api-logs.show', compact('log'));
|
|
}
|
|
|
|
/**
|
|
* 오래된 로그 삭제 (수동)
|
|
*/
|
|
public function prune()
|
|
{
|
|
$deleted = ApiRequestLog::pruneOldLogs();
|
|
|
|
return redirect()->route('dev-tools.api-logs.index')
|
|
->with('success', "{$deleted}개의 로그가 삭제되었습니다.");
|
|
}
|
|
} |