diff --git a/packages/core/data-source-manager/src/data-source.ts b/packages/core/data-source-manager/src/data-source.ts index 84ce8bdb29..49e0e8e1e3 100644 --- a/packages/core/data-source-manager/src/data-source.ts +++ b/packages/core/data-source-manager/src/data-source.ts @@ -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; } diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 065a440794..f29a238332 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -227,7 +227,7 @@ export class Application 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 exten return this._logger; } + get sqlLogger() { + return this._sqlLogger; + } + get log() { return this._logger; } @@ -1083,12 +1087,14 @@ export class Application 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 exten protected closeLogger() { this.log?.close(); this.requestLogger?.close(); - this.sqlLogger?.close(); + this._sqlLogger?.close(); } protected init() { @@ -1209,7 +1215,7 @@ export class Application 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({ diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts index 463c87d318..f4663f2d3c 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/data-source.ts @@ -96,6 +96,7 @@ export class DataSourceModel extends Model { ...createOptions, name: dataSourceKey, logger: app.logger.child({ dataSourceKey }), + sqlLogger: app.sqlLogger.child({ dataSourceKey }), }); if (loadAtAfterStart) {