chore: db logging with sql benchmark

This commit is contained in:
Chareice 2024-11-05 21:19:35 +08:00
parent 13271128b8
commit 5b31c1006b
No known key found for this signature in database
2 changed files with 18 additions and 2 deletions

View File

@ -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

View File

@ -1239,15 +1239,26 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> 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,