構造化リクエストログ
:LiTarget: 何のために?
何か起きた時 access_log なしだと原因特定不能。
:LiSparkle: どう実装する?
middleware で {method, path, status, duration_ms, user_id} を console.log。
:LiCode: コード例
export async function middleware(req: NextRequest) {
const start = Date.now();
const res = await NextResponse.next();
console.log(JSON.stringify({
ts: new Date().toISOString(),
method: req.method,
path: req.nextUrl.pathname,
status: res.status,
duration_ms: Date.now() - start,
ip: req.headers.get('x-forwarded-for'),
}));
return res;
}