- 파라미터 표시: URL 아래에 key=value 형식으로 표시 - 아코디언 기능: row 클릭 시 요청 헤더/응답 바디 확장 표시 - 요청 헤더: 각 항목별 한 줄 포맷 (key: value) - 아이콘 버튼: 상세(눈), AI 복사(복사) 아이콘으로 변경 - AI 복사: 에러(400+) 로그에만 AI 분석 복사 버튼 표시 - 레이아웃: 테이블 컬럼 고정 너비, 셀 중간 정렬 - 아코디언 스타일: 테두리 추가, 배경색 조정 - 모든 로그 삭제(truncate) 기능 추가
117 lines
3.5 KiB
PHP
117 lines
3.5 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()
|
|
->with(['tenant', 'user'])
|
|
->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 . '%');
|
|
}
|
|
|
|
// 필터: 그룹 ID
|
|
if ($request->filled('group_id')) {
|
|
$query->where('group_id', $request->group_id);
|
|
}
|
|
|
|
// 필터: 테넌트
|
|
if ($request->filled('tenant_id')) {
|
|
$query->where('tenant_id', $request->tenant_id);
|
|
}
|
|
|
|
// 통계
|
|
$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();
|
|
|
|
// 현재 페이지 로그들의 그룹별 개수 조회
|
|
$groupIds = $logs->pluck('group_id')->filter()->unique()->values()->toArray();
|
|
$groupCounts = [];
|
|
if (!empty($groupIds)) {
|
|
$groupCounts = ApiRequestLog::whereIn('group_id', $groupIds)
|
|
->selectRaw('group_id, COUNT(*) as count')
|
|
->groupBy('group_id')
|
|
->pluck('count', 'group_id')
|
|
->toArray();
|
|
}
|
|
|
|
return view('api-logs.index', compact('logs', 'stats', 'groupCounts'));
|
|
}
|
|
|
|
/**
|
|
* API 로그 상세 화면
|
|
*/
|
|
public function show(int $id): View
|
|
{
|
|
$log = ApiRequestLog::with(['tenant', 'user'])->findOrFail($id);
|
|
|
|
// 같은 그룹의 다른 요청들
|
|
$groupLogs = [];
|
|
if ($log->group_id) {
|
|
$groupLogs = ApiRequestLog::where('group_id', $log->group_id)
|
|
->where('id', '!=', $log->id)
|
|
->orderByDesc('created_at')
|
|
->get();
|
|
}
|
|
|
|
return view('api-logs.show', compact('log', 'groupLogs'));
|
|
}
|
|
|
|
/**
|
|
* 오래된 로그 삭제 (수동)
|
|
*/
|
|
public function prune()
|
|
{
|
|
$deleted = ApiRequestLog::pruneOldLogs();
|
|
|
|
return redirect()->route('dev-tools.api-logs.index')
|
|
->with('success', "{$deleted}개의 로그가 삭제되었습니다.");
|
|
}
|
|
|
|
/**
|
|
* 모든 로그 삭제
|
|
*/
|
|
public function truncate()
|
|
{
|
|
$deleted = ApiRequestLog::truncate();
|
|
|
|
return redirect()->route('dev-tools.api-logs.index')
|
|
->with('success', '모든 로그가 삭제되었습니다.');
|
|
}
|
|
} |