Skip to content

Commit c2fb3a4

Browse files
committed
feat: add hooks feature + update README
1 parent b3f9621 commit c2fb3a4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,34 @@ flowchart TB
320320
- Efficient context storage with `async_hooks` [AsyncLocalStorage](https://nodejs.org/api/async_hooks.html)
321321
- Minimal overhead compared to standard logging
322322

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+
323351
## 🔌 Integration Support (Platform Agnostic)
324352
- [Fastify](https://fastify.dev/) compatible
325353
- [Express](https://expressjs.com/) compatible
@@ -334,6 +362,7 @@ flowchart TB
334362
| `groupFields` | Object | undefined | Group log fields under specific keys |
335363
| `contextAdapter` | Function | undefined | Transform context before logging |
336364
| `ignoreBootstrapLogs` | boolean | false | Control framework bootstrap logs |
365+
| `hooks` | Object | undefined | Callbacks to execute when logs are created |
337366

338367
# API Reference
339368

0 commit comments

Comments
 (0)