chore: datasource sql logger (#5485)

This commit is contained in:
ChengLei Shao 2024-10-22 17:07:57 +08:00 committed by GitHub
parent 1ee6ca7bef
commit 749b28cef3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 4 deletions

View File

@ -22,6 +22,7 @@ export abstract class DataSource extends EventEmitter {
public resourceManager: ResourceManager;
public acl: ACL;
logger: Logger;
_sqlLogger: Logger;
constructor(protected options: DataSourceOptions) {
super();
@ -32,6 +33,14 @@ export abstract class DataSource extends EventEmitter {
this.logger = logger;
}
setSqlLogger(logger: Logger) {
this._sqlLogger = logger;
}
get sqlLogger() {
return this._sqlLogger || this.logger;
}
get name() {
return this.options.name;
}

View File

@ -227,7 +227,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
*/
public syncManager: SyncManager;
public requestLogger: Logger;
private sqlLogger: Logger;
private _sqlLogger: Logger;
protected _logger: SystemLogger;
constructor(public options: ApplicationOptions) {
@ -252,6 +252,10 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
return this._logger;
}
get sqlLogger() {
return this._sqlLogger;
}
get log() {
return this._logger;
}
@ -1083,12 +1087,14 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
// Due to the use of custom log levels,
// we have to use any type here until Winston updates the type definitions.
}) as any;
this.requestLogger = createLogger({
dirname: getLoggerFilePath(this.name),
filename: 'request',
...(options?.request || {}),
});
this.sqlLogger = this.createLogger({
this._sqlLogger = this.createLogger({
filename: 'sql',
level: 'debug',
});
@ -1097,7 +1103,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
protected closeLogger() {
this.log?.close();
this.requestLogger?.close();
this.sqlLogger?.close();
this._sqlLogger?.close();
}
protected init() {
@ -1209,7 +1215,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
if (msg.includes('INSERT INTO')) {
msg = msg.substring(0, 2000) + '...';
}
this.sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId });
this._sqlLogger.debug({ message: msg, app: this.name, reqId: this.context.reqId });
};
const dbOptions = options.database instanceof Database ? options.database.options : options.database;
const db = new Database({

View File

@ -96,6 +96,7 @@ export class DataSourceModel extends Model {
...createOptions,
name: dataSourceKey,
logger: app.logger.child({ dataSourceKey }),
sqlLogger: app.sqlLogger.child({ dataSourceKey }),
});
if (loadAtAfterStart) {