# event-logger _(no submodules)_ ## Purpose `EventLogger` routes each log entry through an `EventBusLike` emitter after the standard `AbstractLogger` pipeline (level filtering, redaction, sampling, OTel). It exists for environments where other parts of an application need to react to log events — for example, a UI component that surfaces recent errors, a test harness that asserts on emitted entries, or an aggregator that combines logs from multiple services. The emitter is injected so any conformant `EventBusLike` works (including `@suaveplan/events`' `EventBus`). ## Features - Emits `"log"` for every accepted record - Emits `"log:debug"`, `"log:info"`, `"log:warn"`, `"log:error"` per level - Emits `"metrics:updated"` after every log with the current `LoggerMetrics` snapshot - `on*/off*` helper methods for typed subscription/unsubscription - Proper unsubscription via the `() => void` handle returned by `EventBusLike.on` - Compatible with any `EventBusLike>` — no hard dep on `@suaveplan/events` ## Usage: Basic Example ```ts import { EventBus } from "@suaveplan/events"; import { EventLogger } from "@suaveplan/logger/event"; const bus = new EventBus(); const logger = new EventLogger(bus, { level: "info" }); logger.onError((entry) => { alerting.report(entry.message, entry.metadata); }); logger.error("Payment gateway unreachable"); ``` ## Usage: Advanced Example ```ts import { EventBus } from "@suaveplan/events"; import { EventLogger } from "@suaveplan/logger/event"; const bus = new EventBus(); const logger = new EventLogger(bus, { level: "debug" }); // Subscribe const handleInfo = (entry) => console.log("[INFO]", entry.message); logger.onInfo(handleInfo); // Log logger.info("Started"); // Unsubscribe logger.offInfo(handleInfo); ``` ## API Reference ### `class EventLogger extends AbstractLogger` **Constructor**: `new EventLogger(emitter, config?)` | Parameter | Type | Description | |-----------|------|-------------| | `emitter` | `LoggerEventBus` | Any `EventBusLike` conformant bus | | `config` | `EventLoggerConfig?` | Inherits all `AbstractLoggerConfig` options | #### Subscription helpers (all return `this`) `onLog` / `offLog`, `onDebug` / `offDebug`, `onInfo` / `offInfo`, `onWarn` / `offWarn`, `onError` / `offError`, `onMetricsUpdate` / `offMetricsUpdate` #### `getEmitter(): LoggerEventBus` Returns the injected bus. ### `interface LoggerEvents` ```ts { "log": LogEntry; "log:debug": LogEntry; "log:info": LogEntry; "log:warn": LogEntry; "log:error": LogEntry; "metrics:updated": LoggerMetrics; } ``` ## Implementation Notes Unsubscription is implemented by storing the `() => void` cleanup function returned by `emitter.on()` in a `WeakMap`. Calling `off*` looks up and invokes the cleanup. The `WeakMap` avoids memory leaks when the listener function is garbage collected without explicit unsubscription.