Skip to content

Commit c127ca7

Browse files
feat: add custom logger (#29)
* feat: add custom logger * feat: add custom logger * feat: add custom logger * feat: add custom logger
1 parent 2485c13 commit c127ca7

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

README.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,58 @@ export class CustomExceptionListener implements ExceptionListener {
322322
}
323323
```
324324

325+
---
326+
327+
## 📝 Custom Logger
328+
329+
By default, the messaging system uses Nest’s built-in `NestLogger` for logging.
330+
However, you can plug in your own **custom logger** to gain more control over how messages and errors are recorded.
331+
332+
A custom logger must implement the interface `MessagingLogger` interface:
333+
334+
### Providing a custom logger
335+
336+
When configuring the **messaging module**, you can pass your own logger instance via options `customLogger`.
337+
```ts
338+
@Module({
339+
imports: [
340+
MessagingModule.forRoot({
341+
customLogger: new MyCustomLogger(),
342+
...
343+
}),
344+
],
345+
})
346+
export class AppModule {}
347+
```
348+
or if you defined it as provider
349+
```ts
350+
@Module({
351+
imports: [
352+
MessagingModule.forRoot({
353+
customLogger: MyCustomLogger,
354+
...
355+
}),
356+
],
357+
providers: [
358+
MyCustomLogger,
359+
],
360+
})
361+
export class AppModule {}
362+
```
363+
325364
## Configuration options
326365
Here’s a table with the documentation for the `MessagingModule.forRoot` configuration you requested, breaking it into **buses**, **channels** (with descriptions of both channels), and their respective properties, descriptions, and default values:
327366

328367
### `MessagingModule.forRoot` Configuration
329368
<br>
330369

331-
| **Property** | **Description** | **Default Value** |
332-
|----------------|------------------------------------------------------------------------|-------------------------------|
333-
| **`buses`** | Array of message buses that define routing and processing of messages. | `[]` (empty array by default) |
334-
| **`channels`** | Array of channel configurations used by the message buses. | `[]` (empty array by default) |
335-
| **`debug`** | Enables or disables debug mode for logging additional messages. | `false` |
336-
| **`logging`** | Enables or disables logging for bus activity (e.g., message dispatch). | `true` |
337-
370+
| **Property** | **Description** | **Default Value** |
371+
|--------------------|----------------------------------------------------------------------------------|-------------------------------|
372+
| **`buses`** | Array of message buses that define routing and processing of messages. | `[]` (empty array by default) |
373+
| **`channels`** | Array of channel configurations used by the message buses. | `[]` (empty array by default) |
374+
| **`debug`** | Enables or disables debug mode for logging additional messages. | `false` |
375+
| **`logging`** | Enables or disables logging for bus activity (e.g., message dispatch). | `true` |
376+
| **`customLogger`** | Instance of a class implements `MessagingLogger` for custom logging integration. | `NestLogger` |
338377
---
339378

340379
### Buses

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nestjstools/messaging",
3-
"version": "2.21.0",
3+
"version": "2.22.0",
44
"description": "Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.",
55
"author": "Sebastian Iwanczyszyn",
66
"license": "MIT",

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ObjectForwardMessageNormalizer } from './normalizer/object-forward-mess
22
import { Type } from '@nestjs/common';
33
import { DynamicModule } from '@nestjs/common/interfaces/modules/dynamic-module.interface';
44
import { ForwardReference } from '@nestjs/common/interfaces/modules/forward-reference.interface';
5+
import { MessagingLogger } from './logger/messaging-logger';
56

67
export type DefineChannels = ChannelConfig[];
78

@@ -162,4 +163,5 @@ export interface MandatoryMessagingModuleOptions {
162163
debug?: boolean;
163164
logging?: boolean;
164165
global?: boolean;
166+
customLogger?: MessagingLogger | object;
165167
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ export * from './normalizer/message-normalizer';
2828
export * from './exception-listener/exception-listener';
2929
export * from './exception-listener/exception-context';
3030
export * from './exception/handlers.exception';
31+
export * from './logger/log';
32+
export * from './logger/messaging-logger';
33+
export * from './logger/nest-logger';

src/messaging.module.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Logger as NestCommonLogger,
55
Module,
66
OnApplicationBootstrap,
7-
OnApplicationShutdown,
87
OnModuleDestroy,
98
Provider,
109
} from '@nestjs/common';
@@ -20,7 +19,6 @@ import { Service } from './dependency-injection/service';
2019
import { CompositeChannelFactory } from './channel/factory/composite-channel.factory';
2120
import { ChannelRegistry } from './channel/channel.registry';
2221
import { CompositeMessageBusFactory } from './bus/composite-message-bus.factory';
23-
import { MessagingLogger } from './logger/messaging-logger';
2422
import { DistributedMessageBus } from './bus/distributed-message.bus';
2523
import { DiscoveryModule, DiscoveryService, ModuleRef } from '@nestjs/core';
2624
import { InMemoryMessageBus } from './bus/in-memory-message.bus';
@@ -44,6 +42,7 @@ import { NormalizerRegistry } from './normalizer/normalizer.registry';
4442
import { ObjectForwardMessageNormalizer } from './normalizer/object-forward-message.normalizer';
4543
import { ExceptionListenerRegistry } from './exception-listener/exception-listener.registry';
4644
import { ExceptionListenerHandler } from './exception-listener/exception-listener-handler';
45+
import { MessagingLogger } from './logger/messaging-logger';
4746

4847
@Module({})
4948
export class MessagingModule
@@ -168,6 +167,21 @@ export class MessagingModule
168167
};
169168
};
170169

170+
const loggerProvider = (
171+
options.customLogger && typeof options.customLogger === 'function'
172+
? { provide: Service.LOGGER, useClass: options.customLogger }
173+
: {
174+
provide: Service.LOGGER,
175+
useValue:
176+
options.customLogger ??
177+
new NestLogger(
178+
new NestCommonLogger(),
179+
options.debug ?? false,
180+
options.logging ?? true,
181+
),
182+
}
183+
) as Provider;
184+
171185
return {
172186
global: options.global ?? true,
173187
module: MessagingModule,
@@ -203,14 +217,7 @@ export class MessagingModule
203217
},
204218
inject: [Service.CHANNELS, Service.LOGGER],
205219
},
206-
{
207-
provide: Service.LOGGER,
208-
useValue: new NestLogger(
209-
new NestCommonLogger(),
210-
options.debug ?? false,
211-
options.logging ?? true,
212-
),
213-
},
220+
loggerProvider,
214221
HandlerMiddleware,
215222
CompositeChannelFactory,
216223
CompositeMessageBusFactory,

0 commit comments

Comments
 (0)