Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rabbitmq): add custom logger to configuration #401

Merged
22 changes: 22 additions & 0 deletions integration/rabbitmq/e2e/configuration.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -134,4 +135,25 @@ describe('Module Configuration', () => {
expect(spy).toHaveBeenCalledWith(amqplibUri, undefined);
});
});

describe('logger', () => {
it('should use the custom logger', async () => {
const logger = new ConsoleLogger('Custom');
const spy = jest.spyOn(logger, 'log');

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
uri,
connectionInitOptions: { wait: true, reject: true, timeout: 3000 },
logger,
}),
],
}).compile();

expect(app).toBeDefined();

expect(spy).toHaveBeenCalledTimes(1);
});
});
});
10 changes: 8 additions & 2 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const defaultConfig = {

export class AmqpConnection {
private readonly messageSubject = new Subject<CorrelationMessage>();
private readonly logger = new Logger(AmqpConnection.name);
private readonly logger;
private readonly initialized = new Subject<void>();
private _managedConnection!: AmqpConnectionManager;
/**
Expand All @@ -80,7 +80,13 @@ export class AmqpConnection {
private readonly config: Required<RabbitMQConfig>;

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 {
Expand Down
6 changes: 6 additions & 0 deletions packages/rabbitmq/src/rabbitmq.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoggerService } from '@nestjs/common';
import { AmqpConnectionManagerOptions } from 'amqp-connection-manager';
import { Options } from 'amqplib';
import {
Expand Down Expand Up @@ -101,6 +102,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';
Expand Down
4 changes: 2 additions & 2 deletions packages/rabbitmq/src/rabbitmq.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export class RabbitMQModule
static async AmqpConnectionFactory(config: RabbitMQConfig) {
const connection = new AmqpConnection(config);
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'
);
Expand Down