운영 서버 query문 로그 출력 내용 수정

This commit is contained in:
2026-07-05 13:12:49 +09:00
parent c635d64141
commit b9aa6e07a9
+32 -18
View File
@@ -47,24 +47,30 @@ function inlineParams(query: string, rawParams: string): string {
function onQuery(e: Prisma.QueryEvent): void { function onQuery(e: Prisma.QueryEvent): void {
const isSlow = e.duration >= SLOW_QUERY_MS; const isSlow = e.duration >= SLOW_QUERY_MS;
const level = isSlow ? "warn" : "debug";
if (!logger.isEnabled(level)) return;
const isSensitive = e.query.includes("PASSWORD("); const isSensitive = e.query.includes("PASSWORD(");
const sqlToFormat = isSensitive ? e.query : inlineParams(e.query, e.params); const sqlWithParams = isSensitive ? e.query : inlineParams(e.query, e.params);
const formatted = format(sqlToFormat, { language: "mysql", tabWidth: 2 });
const ts = timestamp(); if (isSlow) {
const tag = level.toUpperCase().padEnd(5); const formatted = format(sqlWithParams, { language: "mysql", tabWidth: 2 });
const slowLabel = isSlow ? " [SLOW]" : ""; const ts = timestamp();
const header = `[${ts}] [${tag}] [db:query] ${e.duration}ms${slowLabel}`; const indented = formatted.split("\n").map((l) => ` ${l}`).join("\n");
const indented = formatted writeAll(2, `[${ts}] [WARN ] [db:query] ${e.duration}ms [SLOW]\n${indented}\n`);
.split("\n") return;
.map((l) => ` ${l}`) }
.join("\n");
const out = `${header}\n${indented}\n`; if (logger.isEnabled("debug")) {
writeAll(isSlow ? 2 : 1, out); 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) ────────────────────────────────────────── // ── 결과 로깅 ($extends) ──────────────────────────────────────────
@@ -234,9 +240,17 @@ function createPrismaClient() {
const extendedClient = baseClient.$extends({ const extendedClient = baseClient.$extends({
query: { query: {
$allOperations: async ({ model, operation, args, query }) => { $allOperations: async ({ model, operation, args, query }) => {
const result = await query(args); try {
logResult(model, operation, result); const result = await query(args);
return result; 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;
}
}, },
}, },
}); });