From f5c753bbb2807c1b2be0261ceeb9d31524715182 Mon Sep 17 00:00:00 2001 From: Gabriele Toselli Date: Tue, 23 Apr 2024 09:30:57 +0200 Subject: [PATCH] feat(rabbit-bus): add more logging --- .changeset/good-penguins-sit.md | 5 +++++ packages/ddd-toolkit-rabbit-bus/src/rabbit-connection.ts | 5 ++++- packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.ts | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/good-penguins-sit.md diff --git a/.changeset/good-penguins-sit.md b/.changeset/good-penguins-sit.md new file mode 100644 index 0000000..6888593 --- /dev/null +++ b/.changeset/good-penguins-sit.md @@ -0,0 +1,5 @@ +--- +"@fizzbuds/ddd-toolkit-rabbit-bus": patch +--- + +better logging diff --git a/packages/ddd-toolkit-rabbit-bus/src/rabbit-connection.ts b/packages/ddd-toolkit-rabbit-bus/src/rabbit-connection.ts index fd0d939..4c90639 100644 --- a/packages/ddd-toolkit-rabbit-bus/src/rabbit-connection.ts +++ b/packages/ddd-toolkit-rabbit-bus/src/rabbit-connection.ts @@ -41,7 +41,7 @@ export class RabbitConnection { await this.setupChannel(); await this.setupExchanges(); await this.setupDqlQueue(); - this.logger.debug('Rabbit connection established'); + this.logger.debug(`Rabbit connection established`); } catch (error) { this.logger.error(`Error connection ${inspect(error)}`); throw error; @@ -80,6 +80,7 @@ export class RabbitConnection { private async setupChannel() { this.channel = await this.connection.createConfirmChannel(); await this.channel.prefetch(this.prefetch); + this.logger.debug(`Channel created with prefetch ${this.prefetch}`); this.channel.on('error', async (err) => { if (!this.stopping) return; @@ -95,6 +96,7 @@ export class RabbitConnection { durable: true, }); await this.channel.assertExchange(this.deadLetterExchangeName, 'topic'); + this.logger.debug(`Exchange ${this.exchangeName} asserted`); } private async setupDqlQueue() { @@ -106,5 +108,6 @@ export class RabbitConnection { }, }); await this.channel.bindQueue(this.deadLetterQueueName, this.deadLetterExchangeName, '#'); + this.logger.debug(`Dlq ${this.deadLetterQueueName} asserted`); } } diff --git a/packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.ts b/packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.ts index e4a891f..2320ef2 100644 --- a/packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.ts +++ b/packages/ddd-toolkit-rabbit-bus/src/rabbit-event-bus.ts @@ -59,6 +59,9 @@ export class RabbitEventBus implements IEventBus { await this.connection.getChannel().consume(queueName, (msg) => this.onMessage(msg, queueName)); await this.connection.getChannel().bindQueue(queueName, this.exchangeName, event.name); + this.logger.debug( + `Handler ${handler.constructor.name} subscribed to event ${event.name} on queue ${queueName}`, + ); } public async publish>(event: T): Promise { @@ -66,6 +69,7 @@ export class RabbitEventBus implements IEventBus { const message = Buffer.from(serializedEvent); this.connection.getChannel().publish(this.exchangeName, event.name, message); await this.connection.getChannel().waitForConfirms(); + this.logger.debug(`Event ${event.name} published. ${serializedEvent}`); } public async terminate(): Promise { @@ -103,6 +107,7 @@ export class RabbitEventBus implements IEventBus { try { await handler.handle(event); this.connection.getChannel().ack(rawMessage); + this.logger.debug(`Event ${event.name} handled by ${handler.constructor.name} successfully`); } catch (e) { this.logger.warn(`Error handling message due ${inspect(e)}`); const deliveryCount = rawMessage.properties.headers?.['x-delivery-count'] || 0;