운영 서버 query문 로그 출력 내용 수정
This commit is contained in:
@@ -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;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user