From 8e1eed7c2f7a67c35603454717b98a9cc2d330be Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Fri, 8 Nov 2024 12:13:43 +0800 Subject: [PATCH] feat: database beanchmark (#5615) * chore: db logging with sql benchmark * chore: log response size --- packages/core/database/src/database.ts | 5 +++++ packages/core/logger/src/request-logger.ts | 1 + packages/core/server/src/application.ts | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index a795b44cee..713e2a32eb 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -238,7 +238,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/logger/src/request-logger.ts b/packages/core/logger/src/request-logger.ts index 8875f4c53e..a9670f5827 100644 --- a/packages/core/logger/src/request-logger.ts +++ b/packages/core/logger/src/request-logger.ts @@ -66,6 +66,7 @@ export const requestLogger = (appName: string, requestLogger: Logger, options?: cost, app: appName, reqId, + bodySize: ctx.response.length, }; if (Math.floor(status / 100) == 5) { requestLogger.error({ ...info, res: ctx.body?.['errors'] || ctx.body }); diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index f29a238332..95348473ff 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -1208,15 +1208,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,