diff --git a/integration/rabbitmq/e2e/configuration.e2e-spec.ts b/integration/rabbitmq/e2e/configuration.e2e-spec.ts index 904f4f123..9d86ef3c4 100644 --- a/integration/rabbitmq/e2e/configuration.e2e-spec.ts +++ b/integration/rabbitmq/e2e/configuration.e2e-spec.ts @@ -1,4 +1,5 @@ import { RabbitMQConfig, RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; +import { ConsoleLogger } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import * as amqplib from 'amqplib'; @@ -8,6 +9,7 @@ const rabbitPort = process.env.NODE_ENV === 'ci' ? process.env.RABBITMQ_PORT : '5672'; const uri = `amqp://rabbitmq:rabbitmq@${rabbitHost}:${rabbitPort}`; const amqplibUri = `${uri}?heartbeat=5`; +const logger = new ConsoleLogger('Custom logger'); class RabbitConfig { createModuleConfig(): RabbitMQConfig { @@ -30,12 +32,14 @@ describe('Module Configuration', () => { describe('forRoot', () => { it('should configure RabbitMQ', async () => { const spy = jest.spyOn(amqplib, 'connect'); + const logSpy = jest.spyOn(logger, 'log'); app = await Test.createTestingModule({ imports: [ RabbitMQModule.forRoot(RabbitMQModule, { uri, connectionInitOptions: { wait: true, reject: true, timeout: 3000 }, + logger, }), ], }).compile(); @@ -44,6 +48,8 @@ describe('Module Configuration', () => { expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith(amqplibUri, undefined); + + expect(logSpy).toHaveBeenCalled(); }); }); diff --git a/packages/rabbitmq/src/amqp/connection.ts b/packages/rabbitmq/src/amqp/connection.ts index 89d5497c1..bfa4cf364 100644 --- a/packages/rabbitmq/src/amqp/connection.ts +++ b/packages/rabbitmq/src/amqp/connection.ts @@ -1,4 +1,4 @@ -import { Logger } from '@nestjs/common'; +import { Logger, LoggerService } from '@nestjs/common'; import { ChannelWrapper, AmqpConnectionManager, @@ -64,7 +64,7 @@ const defaultConfig = { export class AmqpConnection { private readonly messageSubject = new Subject(); - private readonly logger = new Logger(AmqpConnection.name); + private readonly logger: LoggerService; private readonly initialized = new Subject(); private _managedConnection!: AmqpConnectionManager; /** @@ -81,7 +81,13 @@ export class AmqpConnection { private readonly config: Required; constructor(config: RabbitMQConfig) { - this.config = { ...defaultConfig, ...config }; + this.config = { + logger: config.logger || new Logger(AmqpConnection.name), + ...defaultConfig, + ...config, + }; + + this.logger = this.config.logger; } get channel(): Channel { diff --git a/packages/rabbitmq/src/rabbitmq.interfaces.ts b/packages/rabbitmq/src/rabbitmq.interfaces.ts index d134dcff4..90112d0cb 100644 --- a/packages/rabbitmq/src/rabbitmq.interfaces.ts +++ b/packages/rabbitmq/src/rabbitmq.interfaces.ts @@ -1,3 +1,4 @@ +import { LoggerService } from '@nestjs/common'; import { AmqpConnectionManagerOptions } from 'amqp-connection-manager'; import { Options } from 'amqplib'; import { @@ -103,6 +104,11 @@ export interface RabbitMQConfig { * By setting `prefetchCount` for a channel, you can manage message speeds of your various handlers on the same connection. */ channels?: RabbitMQChannels; + + /** + * You can pass your implementation of the Nestjs LoggerService. + */ + logger?: LoggerService; } export type RabbitHandlerType = 'rpc' | 'subscribe'; diff --git a/packages/rabbitmq/src/rabbitmq.module.ts b/packages/rabbitmq/src/rabbitmq.module.ts index 297584a83..fc63657ef 100644 --- a/packages/rabbitmq/src/rabbitmq.module.ts +++ b/packages/rabbitmq/src/rabbitmq.module.ts @@ -81,13 +81,13 @@ export class RabbitMQModule const connection = new AmqpConnection(config); this.connectionManager.addConnection(connection); await connection.init(); - const logger = new Logger(RabbitMQModule.name); + const logger = config.logger || new Logger(RabbitMQModule.name); logger.log('Successfully connected to RabbitMQ'); return connection; } public static build(config: RabbitMQConfig): DynamicModule { - const logger = new Logger(RabbitMQModule.name); + const logger = config.logger || new Logger(RabbitMQModule.name); logger.warn( 'build() is deprecated. use forRoot() or forRootAsync() to configure RabbitMQ' );