From ca03491301c155da2180b29cab144c8a5f84485b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8A=E3=81=95=E3=82=80=E3=81=AE=E3=81=B2=E3=81=A8?= <46447427+samunohito@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:00:15 +0900 Subject: [PATCH] =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/postgres.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index a680ddd28f..df865b2a01 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -5,9 +5,8 @@ // https://github.com/typeorm/typeorm/issues/2400 import pg from 'pg'; -import { DataSource, Logger } from 'typeorm'; +import { DataSource, Logger, type QueryRunner } from 'typeorm'; import * as highlight from 'cli-highlight'; -import { type QueryRunner } from 'typeorm'; import { entities as charts } from '@/core/chart/entities.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; @@ -97,32 +96,36 @@ class MyCustomLogger implements Logger { } @bindThis - private highlight(sql: string, queryRunner?: QueryRunner) { - const result = highlight.highlight(sql, { + private highlight(sql: string) { + return highlight.highlight(sql, { language: 'sql', ignoreIllegals: true, }); + } - if (this.printReplicationMode && queryRunner) { - const mode = queryRunner.getReplicationMode(); - return `[${mode}] ${result}`; + @bindThis + private appendPrefixIfNeeded(message: string, opts?: { + queryRunner?: QueryRunner; + }): string { + if (this.printReplicationMode && opts?.queryRunner) { + return `[${opts.queryRunner.getReplicationMode()}] ${message}`; } else { - return result; + return message; } } @bindThis public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.info(this.highlight(query, queryRunner).substring(0, 100)); + sqlLogger.info(this.appendPrefixIfNeeded(this.highlight(query).substring(0, 100), { queryRunner })); } @bindThis public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.error(this.highlight(query, queryRunner)); + sqlLogger.error(this.appendPrefixIfNeeded(this.highlight(query), { queryRunner })); } @bindThis public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.warn(this.highlight(query, queryRunner)); + sqlLogger.warn(this.appendPrefixIfNeeded(this.highlight(query), { queryRunner })); } @bindThis