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
6 changes: 6 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 All @@ -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 {
Expand All @@ -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();
Expand All @@ -44,6 +48,8 @@ describe('Module Configuration', () => {

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(amqplibUri, undefined);

expect(logSpy).toHaveBeenCalled();
});
});

Expand Down
12 changes: 9 additions & 3 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from '@nestjs/common';
import { Logger, LoggerService } from '@nestjs/common';
import {
ChannelWrapper,
AmqpConnectionManager,
Expand Down Expand Up @@ -64,7 +64,7 @@ const defaultConfig = {

export class AmqpConnection {
private readonly messageSubject = new Subject<CorrelationMessage>();
private readonly logger = new Logger(AmqpConnection.name);
private readonly logger: LoggerService;
private readonly initialized = new Subject<void>();
private _managedConnection!: AmqpConnectionManager;
/**
Expand All @@ -81,7 +81,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 @@ -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';
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 @@ -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'
);
Expand Down