@@ -320,6 +320,34 @@ flowchart TB
320
320
- Efficient context storage with ` async_hooks ` [ AsyncLocalStorage] ( https://nodejs.org/api/async_hooks.html )
321
321
- Minimal overhead compared to standard logging
322
322
323
+ ## 🪝 Hooks
324
+ - Execute callbacks when logs are created
325
+ - Support for level-specific hooks (` log ` , ` debug ` , ` warn ` , ` error ` )
326
+ - Global hooks that run for all log levels
327
+ - Useful for metrics, external reporting, or custom side effects
328
+
329
+ Example usage:
330
+ ``` typescript
331
+ ContextLoggerModule .forRoot ({
332
+ hooks: {
333
+ // Run for all log levels
334
+ all: [
335
+ (message , bindings ) => {
336
+ metrics .increment (' log.count' );
337
+ }
338
+ ],
339
+ // Run only for errors
340
+ error: [
341
+ (message , bindings ) => {
342
+ errorReporting .notify (message , bindings );
343
+ }
344
+ ]
345
+ }
346
+ })
347
+ ```
348
+
349
+ ⚠️ Note: Hooks are executed synchronously and sequentially. Use with caution as they can introduce latency to the logging process.
350
+
323
351
## 🔌 Integration Support (Platform Agnostic)
324
352
- [ Fastify] ( https://fastify.dev/ ) compatible
325
353
- [ Express] ( https://expressjs.com/ ) compatible
@@ -334,6 +362,7 @@ flowchart TB
334
362
| ` groupFields ` | Object | undefined | Group log fields under specific keys |
335
363
| ` contextAdapter ` | Function | undefined | Transform context before logging |
336
364
| ` ignoreBootstrapLogs ` | boolean | false | Control framework bootstrap logs |
365
+ | ` hooks ` | Object | undefined | Callbacks to execute when logs are created |
337
366
338
367
# API Reference
339
368
0 commit comments