웹 로그, DB 쿼리문과 결과 로그 출력

This commit is contained in:
2026-06-22 22:34:56 +09:00
parent bcd0ef2af3
commit f384388c67
21 changed files with 706 additions and 214 deletions
+54
View File
@@ -0,0 +1,54 @@
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import { ApiError } from "@/lib/errors";
import { logger } from "@/lib/logger";
export type ApiHandlerContext<P> = {
params: Promise<P>;
t0: number;
};
export function withApiHandler<P = Record<string, never>>(
ctx: string,
handler: (req: Request, ctx: ApiHandlerContext<P>) => Promise<NextResponse>
) {
return async (
request: Request,
routeContext?: { params: Promise<P> }
): Promise<NextResponse> => {
const t0 = Date.now();
const session = await auth();
if (!session) {
logger.warn(ctx, "unauthorized");
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
const handlerCtx: ApiHandlerContext<P> = {
params: routeContext?.params ?? (Promise.resolve({}) as Promise<P>),
t0,
};
try {
return await handler(request, handlerCtx);
} catch (error) {
if (error instanceof ApiError) {
const level = error.statusCode >= 500 ? "error" : "warn";
logger[level](ctx, error.message, {
status: error.statusCode,
duration: Date.now() - t0,
});
return NextResponse.json(
{ message: error.message },
{ status: error.statusCode }
);
}
logger.error(ctx, "unexpected error", {
error: String(error),
duration: Date.now() - t0,
});
throw error;
}
};
}