import { Logger, QueryRunner } from "typeorm"; import * as fs from "fs"; import * as path from "path"; interface IOutput { initialize(): void; write(message: string): void; } class ConsoleOutput implements IOutput { initialize(): void { // do nothing } write(message: string): void { console.log(message); } } class FileOutput implements IOutput { private logPath = path.join("/app/dictation_function/.test", "logs"); private fileName = new Date().getTime(); initialize(): void { if (!fs.existsSync(this.logPath)) { fs.mkdirSync(this.logPath, { recursive: true }); } } write(message: string): void { const logFile = path.join(this.logPath, `${this.fileName}.log`); fs.appendFileSync(logFile, `${message}\n`); } } class NoneOutput implements IOutput { initialize(): void { // do nothing } write(message: string): void { // do nothing } } export class TestLogger implements Logger { out: IOutput; constructor(output: "none" | "file" | "console") { switch (output) { case "none": this.out = new NoneOutput(); break; case "file": this.out = new FileOutput(); break; case "console": this.out = new ConsoleOutput(); break; default: this.out = new NoneOutput(); break; } this.out.initialize(); } private write(message: string): void { this.out.write(message); } logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { const raw = `Query: ${query} -- Parameters: ${JSON.stringify(parameters)}`; // ex: 2024-03-08T06:38:43.125Z を TIME という文字列に置換 const dateRemoved = raw.replace( /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g, "TIME" ); // ex: /* コメント内容 */ を /* コメント */ という文字列に置換 const commentRemoved = dateRemoved.replace( /\/\*.*\*\//g, "/* RequestID */" ); // UUIDを固定文字列に置換する ex: 88a9c78e-115a-439c-9e23-731d649f0c27 を XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX という文字列に置換 const uuidRemoved = commentRemoved.replace( /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/g, "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ); this.write(uuidRemoved); } logQueryError( error: string, query: string, parameters?: any[], queryRunner?: QueryRunner ) { this.write( `ERROR: ${error} -- Query: ${query} -- Parameters: ${JSON.stringify( parameters )}` ); } logQuerySlow( time: number, query: string, parameters?: any[], queryRunner?: QueryRunner ) { this.write( `SLOW QUERY: ${time}ms -- Query: ${query} -- Parameters: ${JSON.stringify( parameters )}` ); } logSchemaBuild(message: string, queryRunner?: QueryRunner) { this.write(`Schema Build: ${message}`); } logMigration(message: string, queryRunner?: QueryRunner) { this.write(`Migration: ${message}`); } log(level: "log" | "info" | "warn", message: any, queryRunner?: QueryRunner) { this.write(`${level.toUpperCase()}: ${message}`); } }