From 5b31c1006bd066277b3e650faf0ee8faba63c697 Mon Sep 17 00:00:00 2001 From: Chareice Date: Tue, 5 Nov 2024 21:19:35 +0800 Subject: [PATCH] chore: db logging with sql benchmark --- packages/core/database/src/database.ts | 5 +++++ packages/core/server/src/application.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index dedfc03280..d3d321676f 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -234,7 +234,12 @@ export class Database extends EventEmitter implements AsyncEmitter { }); } + if (options.logging && process.env['DB_SQL_BENCHMARK'] == 'true') { + opts.benchmark = true; + } + this.options = opts; + this.logger.debug( `create database instance: ${safeJsonStringify( // remove sensitive information diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index c5635d8e2f..85131a3655 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -1239,15 +1239,26 @@ export class Application exten } protected createDatabase(options: ApplicationOptions) { - const logging = (msg: any) => { + const logging = (...args) => { + let msg = args[0]; + if (typeof msg === 'string') { msg = msg.replace(/[\r\n]/gm, '').replace(/\s+/g, ' '); } + if (msg.includes('INSERT INTO')) { msg = msg.substring(0, 2000) + '...'; } - this._sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId }); + + const content: any = { message: msg, app: this.name, reqId: this.context.reqId }; + + if (args[1] && typeof args[1] === 'number') { + content.executeTime = args[1]; + } + + this._sqlLogger.debug(content); }; + const dbOptions = options.database instanceof Database ? options.database.options : options.database; const db = new Database({ ...dbOptions,