From b9aa6e07a902365bd008685e62222f81eb61c4e1 Mon Sep 17 00:00:00 2001 From: jisangs Date: Sun, 5 Jul 2026 13:12:49 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9A=B4=EC=98=81=20=EC=84=9C=EB=B2=84=20query?= =?UTF-8?q?=EB=AC=B8=20=EB=A1=9C=EA=B7=B8=20=EC=B6=9C=EB=A0=A5=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db.ts | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/db.ts b/lib/db.ts index 695f6aa..897059e 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -47,24 +47,30 @@ function inlineParams(query: string, rawParams: string): string { function onQuery(e: Prisma.QueryEvent): void { const isSlow = e.duration >= SLOW_QUERY_MS; - const level = isSlow ? "warn" : "debug"; - if (!logger.isEnabled(level)) return; - const isSensitive = e.query.includes("PASSWORD("); - const sqlToFormat = isSensitive ? e.query : inlineParams(e.query, e.params); - const formatted = format(sqlToFormat, { language: "mysql", tabWidth: 2 }); + const sqlWithParams = isSensitive ? e.query : inlineParams(e.query, e.params); - const ts = timestamp(); - const tag = level.toUpperCase().padEnd(5); - const slowLabel = isSlow ? " [SLOW]" : ""; - const header = `[${ts}] [${tag}] [db:query] ${e.duration}ms${slowLabel}`; - const indented = formatted - .split("\n") - .map((l) => ` ${l}`) - .join("\n"); + if (isSlow) { + const formatted = format(sqlWithParams, { language: "mysql", tabWidth: 2 }); + const ts = timestamp(); + const indented = formatted.split("\n").map((l) => ` ${l}`).join("\n"); + writeAll(2, `[${ts}] [WARN ] [db:query] ${e.duration}ms [SLOW]\n${indented}\n`); + return; + } - const out = `${header}\n${indented}\n`; - writeAll(isSlow ? 2 : 1, out); + if (logger.isEnabled("debug")) { + const formatted = format(sqlWithParams, { language: "mysql", tabWidth: 2 }); + const ts = timestamp(); + const indented = formatted.split("\n").map((l) => ` ${l}`).join("\n"); + writeAll(1, `[${ts}] [DEBUG] [db:query] ${e.duration}ms\n${indented}\n`); + return; + } + + if (logger.isEnabled("info")) { + const sql = sqlWithParams.replace(/\s+/g, " ").trim(); + const ts = timestamp(); + writeAll(1, `[${ts}] [INFO ] [db:query] ${e.duration}ms ${sql}\n`); + } } // ── 결과 로깅 ($extends) ────────────────────────────────────────── @@ -234,9 +240,17 @@ function createPrismaClient() { const extendedClient = baseClient.$extends({ query: { $allOperations: async ({ model, operation, args, query }) => { - const result = await query(args); - logResult(model, operation, result); - return result; + try { + const result = await query(args); + logResult(model, operation, result); + return result; + } catch (err) { + const label = model ? `${model}.${operation}` : operation; + const msg = err instanceof Error ? err.message : String(err); + const ts = timestamp(); + writeAll(2, `[${ts}] [ERROR] [db:error] ${label} failed: ${msg}\n`); + throw err; + } }, }, });