This commit is contained in:
おさむのひと 2024-12-10 19:00:15 +09:00
parent 72e6adf5ab
commit ca03491301

View File

@ -5,9 +5,8 @@
// https://github.com/typeorm/typeorm/issues/2400 // https://github.com/typeorm/typeorm/issues/2400
import pg from 'pg'; import pg from 'pg';
import { DataSource, Logger } from 'typeorm'; import { DataSource, Logger, type QueryRunner } from 'typeorm';
import * as highlight from 'cli-highlight'; import * as highlight from 'cli-highlight';
import { type QueryRunner } from 'typeorm';
import { entities as charts } from '@/core/chart/entities.js'; import { entities as charts } from '@/core/chart/entities.js';
import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js';
@ -97,32 +96,36 @@ class MyCustomLogger implements Logger {
} }
@bindThis @bindThis
private highlight(sql: string, queryRunner?: QueryRunner) { private highlight(sql: string) {
const result = highlight.highlight(sql, { return highlight.highlight(sql, {
language: 'sql', ignoreIllegals: true, language: 'sql', ignoreIllegals: true,
}); });
}
if (this.printReplicationMode && queryRunner) { @bindThis
const mode = queryRunner.getReplicationMode(); private appendPrefixIfNeeded(message: string, opts?: {
return `[${mode}] ${result}`; queryRunner?: QueryRunner;
}): string {
if (this.printReplicationMode && opts?.queryRunner) {
return `[${opts.queryRunner.getReplicationMode()}] ${message}`;
} else { } else {
return result; return message;
} }
} }
@bindThis @bindThis
public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis
public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis
public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis