Skip to content

Commit c09bdab

Browse files
authored
feat(core): add control over logging debug messages (#445)
1 parent 77f8609 commit c09bdab

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

docs/docs/04_api/02_module-options.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ All of the **`Cls{Middleware,Guard,Interceptor}Options`** take the following par
7575
- **_`mount?:`_**`boolean` (default _`false`_)
7676
Whether to automatically mount the middleware/guard/interceptor to every route (not applicable when instantiating them manually)
7777

78+
- **_`debug?:`_**`boolean` (default _`true`_)
79+
Enable logging of debug messages in the middleware/guard/interceptor.
80+
7881
- **_`generateId?:`_**`boolean` (default _`false`_)
7982
Whether to automatically generate a request ID. It will be available under the `CLS_ID` key.
8083

packages/core/src/lib/cls-module/cls-root.module.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ export class ClsRootModule implements NestModule, OnModuleInit {
6161
if (options.mount) {
6262
const adapter = this.adapterHost.httpAdapter;
6363
const mountPoint = getMiddlewareMountPoint(adapter);
64-
ClsRootModule.logger.debug(
65-
'Mounting ClsMiddleware to ' + mountPoint,
66-
);
64+
if (options.debug) {
65+
ClsRootModule.logger.debug(
66+
'Mounting ClsMiddleware to ' + mountPoint,
67+
);
68+
}
6769
consumer.apply(ClsMiddleware).forRoutes(mountPoint);
6870
}
6971
}
@@ -215,9 +217,11 @@ export class ClsRootModule implements NestModule, OnModuleInit {
215217

216218
private static clsGuardFactory(options: ClsGuardOptions): CanActivate {
217219
if (options.mount) {
218-
ClsRootModule.logger.debug(
219-
'ClsGuard will be automatically mounted',
220-
);
220+
if (options.debug) {
221+
ClsRootModule.logger.debug(
222+
'ClsGuard will be automatically mounted',
223+
);
224+
}
221225
return new ClsGuard(options);
222226
}
223227
return {
@@ -229,9 +233,11 @@ export class ClsRootModule implements NestModule, OnModuleInit {
229233
options: ClsInterceptorOptions,
230234
): NestInterceptor {
231235
if (options.mount) {
232-
ClsRootModule.logger.debug(
233-
'ClsInterceptor will be automatically mounted',
234-
);
236+
if (options.debug) {
237+
ClsRootModule.logger.debug(
238+
'ClsInterceptor will be automatically mounted',
239+
);
240+
}
235241
return new ClsInterceptor(options);
236242
}
237243
return {

packages/core/src/lib/cls.options.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ export class ClsMiddlewareOptions extends ClsInitializerCommonOptions {
113113
*/
114114
mount? = false;
115115

116+
/**
117+
* enable logging of debug messages in the middleware
118+
*
119+
* Default: `true`
120+
*/
121+
debug? = true;
122+
116123
/**
117124
* the function to generate request ids for the CLS context
118125
*/
@@ -155,6 +162,13 @@ export class ClsGuardOptions extends ClsInitializerCommonOptions {
155162
*/
156163
mount? = false;
157164

165+
/**
166+
* enable logging of debug messages in guard
167+
*
168+
* Default: `true`
169+
*/
170+
debug? = true;
171+
158172
/**
159173
* the function to generate request ids inside the guard
160174
*/
@@ -187,6 +201,13 @@ export class ClsInterceptorOptions extends ClsInitializerCommonOptions {
187201
*/
188202
mount? = false;
189203

204+
/**
205+
* enable logging of debug messages in the interceptor
206+
*
207+
* Default: `true`
208+
*/
209+
debug? = true;
210+
190211
/**
191212
* the function to generate request ids inside the interceptor
192213
*/

0 commit comments

Comments
 (0)