@warlock.js/logger 4.0.174 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/README.md +145 -422
  2. package/cjs/index.cjs +1003 -0
  3. package/cjs/index.cjs.map +1 -0
  4. package/esm/channels/console-log.d.mts +40 -0
  5. package/esm/channels/console-log.d.mts.map +1 -0
  6. package/esm/channels/console-log.mjs +51 -0
  7. package/esm/channels/console-log.mjs.map +1 -0
  8. package/esm/channels/file-log.d.mts +194 -0
  9. package/esm/channels/file-log.d.mts.map +1 -0
  10. package/esm/channels/file-log.mjs +267 -0
  11. package/esm/channels/file-log.mjs.map +1 -0
  12. package/esm/channels/index.mjs +5 -0
  13. package/esm/channels/json-file-log.d.mts +33 -0
  14. package/esm/channels/json-file-log.d.mts.map +1 -0
  15. package/esm/channels/json-file-log.mjs +137 -0
  16. package/esm/channels/json-file-log.mjs.map +1 -0
  17. package/esm/index.d.mts +11 -0
  18. package/esm/index.mjs +13 -0
  19. package/esm/log-channel.d.mts +78 -0
  20. package/esm/log-channel.d.mts.map +1 -0
  21. package/esm/log-channel.mjs +75 -0
  22. package/esm/log-channel.mjs.map +1 -0
  23. package/esm/logger.d.mts +184 -0
  24. package/esm/logger.d.mts.map +1 -0
  25. package/esm/logger.mjs +282 -0
  26. package/esm/logger.mjs.map +1 -0
  27. package/esm/redact/redact.d.mts +25 -0
  28. package/esm/redact/redact.d.mts.map +1 -0
  29. package/esm/redact/redact.mjs +109 -0
  30. package/esm/redact/redact.mjs.map +1 -0
  31. package/esm/types.d.mts +129 -0
  32. package/esm/types.d.mts.map +1 -0
  33. package/esm/utils/capture-unhandled-errors.d.mts +16 -0
  34. package/esm/utils/capture-unhandled-errors.d.mts.map +1 -0
  35. package/esm/utils/capture-unhandled-errors.mjs +26 -0
  36. package/esm/utils/capture-unhandled-errors.mjs.map +1 -0
  37. package/esm/utils/clear-message.d.mts +8 -0
  38. package/esm/utils/clear-message.d.mts.map +1 -0
  39. package/esm/utils/clear-message.mjs +12 -0
  40. package/esm/utils/clear-message.mjs.map +1 -0
  41. package/esm/utils/index.mjs +5 -0
  42. package/esm/utils/safe-json-stringify.d.mts +14 -0
  43. package/esm/utils/safe-json-stringify.d.mts.map +1 -0
  44. package/esm/utils/safe-json-stringify.mjs +35 -0
  45. package/esm/utils/safe-json-stringify.mjs.map +1 -0
  46. package/llms-full.txt +1296 -0
  47. package/llms.txt +19 -0
  48. package/package.json +39 -39
  49. package/skills/capture-unhandled-errors/SKILL.md +103 -0
  50. package/skills/configure-logger/SKILL.md +105 -0
  51. package/skills/filter-log-entries/SKILL.md +120 -0
  52. package/skills/flush-logs-on-shutdown/SKILL.md +91 -0
  53. package/skills/logger-basics/SKILL.md +85 -0
  54. package/skills/overview/SKILL.md +86 -0
  55. package/skills/pick-log-channel/SKILL.md +139 -0
  56. package/skills/redact-sensitive-log-fields/SKILL.md +122 -0
  57. package/skills/test-logging-code/SKILL.md +169 -0
  58. package/skills/use-log-helpers/SKILL.md +66 -0
  59. package/skills/write-custom-log-channel/SKILL.md +160 -0
  60. package/cjs/channels/console-log.d.ts +0 -17
  61. package/cjs/channels/console-log.d.ts.map +0 -1
  62. package/cjs/channels/console-log.js +0 -47
  63. package/cjs/channels/console-log.js.map +0 -1
  64. package/cjs/channels/file-log.d.ts +0 -171
  65. package/cjs/channels/file-log.d.ts.map +0 -1
  66. package/cjs/channels/file-log.js +0 -293
  67. package/cjs/channels/file-log.js.map +0 -1
  68. package/cjs/channels/index.d.ts +0 -4
  69. package/cjs/channels/index.d.ts.map +0 -1
  70. package/cjs/channels/json-file-log.d.ts +0 -33
  71. package/cjs/channels/json-file-log.d.ts.map +0 -1
  72. package/cjs/channels/json-file-log.js +0 -164
  73. package/cjs/channels/json-file-log.js.map +0 -1
  74. package/cjs/index.d.ts +0 -6
  75. package/cjs/index.d.ts.map +0 -1
  76. package/cjs/index.js +0 -1
  77. package/cjs/index.js.map +0 -1
  78. package/cjs/log-channel.d.ts +0 -67
  79. package/cjs/log-channel.d.ts.map +0 -1
  80. package/cjs/log-channel.js +0 -88
  81. package/cjs/log-channel.js.map +0 -1
  82. package/cjs/logger.d.ts +0 -62
  83. package/cjs/logger.d.ts.map +0 -1
  84. package/cjs/logger.js +0 -124
  85. package/cjs/logger.js.map +0 -1
  86. package/cjs/types.d.ts +0 -104
  87. package/cjs/types.d.ts.map +0 -1
  88. package/cjs/utils/capture-unhandled-errors.d.ts +0 -2
  89. package/cjs/utils/capture-unhandled-errors.d.ts.map +0 -1
  90. package/cjs/utils/capture-unhandled-errors.js +0 -12
  91. package/cjs/utils/capture-unhandled-errors.js.map +0 -1
  92. package/cjs/utils/clear-message.d.ts +0 -5
  93. package/cjs/utils/clear-message.d.ts.map +0 -1
  94. package/cjs/utils/clear-message.js +0 -9
  95. package/cjs/utils/clear-message.js.map +0 -1
  96. package/cjs/utils/index.d.ts +0 -3
  97. package/cjs/utils/index.d.ts.map +0 -1
  98. package/esm/channels/console-log.d.ts +0 -17
  99. package/esm/channels/console-log.d.ts.map +0 -1
  100. package/esm/channels/console-log.js +0 -47
  101. package/esm/channels/console-log.js.map +0 -1
  102. package/esm/channels/file-log.d.ts +0 -171
  103. package/esm/channels/file-log.d.ts.map +0 -1
  104. package/esm/channels/file-log.js +0 -293
  105. package/esm/channels/file-log.js.map +0 -1
  106. package/esm/channels/index.d.ts +0 -4
  107. package/esm/channels/index.d.ts.map +0 -1
  108. package/esm/channels/json-file-log.d.ts +0 -33
  109. package/esm/channels/json-file-log.d.ts.map +0 -1
  110. package/esm/channels/json-file-log.js +0 -164
  111. package/esm/channels/json-file-log.js.map +0 -1
  112. package/esm/index.d.ts +0 -6
  113. package/esm/index.d.ts.map +0 -1
  114. package/esm/index.js +0 -1
  115. package/esm/index.js.map +0 -1
  116. package/esm/log-channel.d.ts +0 -67
  117. package/esm/log-channel.d.ts.map +0 -1
  118. package/esm/log-channel.js +0 -88
  119. package/esm/log-channel.js.map +0 -1
  120. package/esm/logger.d.ts +0 -62
  121. package/esm/logger.d.ts.map +0 -1
  122. package/esm/logger.js +0 -124
  123. package/esm/logger.js.map +0 -1
  124. package/esm/types.d.ts +0 -104
  125. package/esm/types.d.ts.map +0 -1
  126. package/esm/utils/capture-unhandled-errors.d.ts +0 -2
  127. package/esm/utils/capture-unhandled-errors.d.ts.map +0 -1
  128. package/esm/utils/capture-unhandled-errors.js +0 -12
  129. package/esm/utils/capture-unhandled-errors.js.map +0 -1
  130. package/esm/utils/clear-message.d.ts +0 -5
  131. package/esm/utils/clear-message.d.ts.map +0 -1
  132. package/esm/utils/clear-message.js +0 -9
  133. package/esm/utils/clear-message.js.map +0 -1
  134. package/esm/utils/index.d.ts +0 -3
  135. package/esm/utils/index.d.ts.map +0 -1
package/llms-full.txt ADDED
@@ -0,0 +1,1296 @@
1
+ # Warlock Logger — full skills
2
+
3
+ > Package: `@warlock.js/logger`
4
+
5
+ > Generated artifact. Concatenates every SKILL.md and reference file under `@warlock.js/logger/skills/`. Re-run `node scripts/generate-llms.mjs` after any change.
6
+
7
+ ## capture-unhandled-errors `@warlock.js/logger/capture-unhandled-errors/SKILL.md`
8
+
9
+ ---
10
+ name: capture-unhandled-errors
11
+ description: 'captureAnyUnhandledRejection() installs process.on(''unhandledRejection'') + (''uncaughtException'') listeners routing failures through log.error(''app'', ...). Triggers: `captureAnyUnhandledRejection`, `unhandledRejection`, `uncaughtException`, `log.error`; "log unhandled promise rejections", "catch uncaught exceptions to a file", "record crashes before exit", "global error handler with logger"; typical import `import { captureAnyUnhandledRejection, log } from "@warlock.js/logger"`. Skip: flushing — `@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`; filtering — `@warlock.js/logger/filter-log-entries/SKILL.md`; competing `Sentry.init`, `@sentry/node`; native `process.on(''unhandledRejection'')`.'
12
+ ---
13
+
14
+ # Error capture — routing Node's unhandled errors through the logger
15
+
16
+ `captureAnyUnhandledRejection()` installs two process-level listeners so crashes are logged (not silently swallowed) before Node exits.
17
+
18
+ ## What it does
19
+
20
+ ```ts
21
+ import { captureAnyUnhandledRejection } from "@warlock.js/logger";
22
+
23
+ captureAnyUnhandledRejection();
24
+ ```
25
+
26
+ Registers:
27
+ - `process.on("unhandledRejection", reason => log.error("app", "unhandledRejection", reason))`
28
+ - `process.on("uncaughtException", error => log.error("app", "uncaughtException", error))`
29
+
30
+ Nothing else — the failure goes through `log.error` only, so it lands in your configured channels rather than bypassing them with a raw `console.log`.
31
+
32
+ ## When to call it
33
+
34
+ **Once**, at startup, **after** channels are registered. Typical place: immediately after your `log.configure({...})` call.
35
+
36
+ ```ts title="src/index.ts"
37
+ import {
38
+ log,
39
+ ConsoleLog,
40
+ FileLog,
41
+ captureAnyUnhandledRejection,
42
+ } from "@warlock.js/logger";
43
+
44
+ log.configure({
45
+ channels: [new ConsoleLog(), new FileLog({ levels: ["error"] })],
46
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"], // ← important; see below
47
+ });
48
+
49
+ captureAnyUnhandledRejection();
50
+ ```
51
+
52
+ ## Pair with `autoFlushOn: ["beforeExit"]`
53
+
54
+ Without a flush on exit, here's what happens on a crash:
55
+
56
+ 1. Promise rejection fires → `log.error(...)` queues the error into `FileLog`'s buffer.
57
+ 2. Node exits.
58
+ 3. Buffer is never flushed. **The error that killed your app is lost.**
59
+
60
+ Including `"beforeExit"` in `autoFlushOn` closes the gap. Node fires `beforeExit` after the rejection handler resolves, the logger flushes, then Node exits. See [`@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md).
61
+
62
+ ## Idempotency — don't call it twice
63
+
64
+ Calling `captureAnyUnhandledRejection()` a second time registers a second pair of listeners. Your next rejection gets logged twice. There's no dedup; just call it once.
65
+
66
+ ## What it does **not** do
67
+
68
+ - **Does not swallow errors.** Node still exits after `uncaughtException` (this is the safe behavior — state is undefined). The logger just ensures the error is recorded first.
69
+ - **Does not install Node's `--unhandled-rejections` policy.** That's a Node flag; set it in your launch script if you want strict mode.
70
+ - **Does not hook `SIGTERM` / `SIGINT`** — use `enableAutoFlush` for signal flushes.
71
+ - **Does not filter.** Every rejection/exception is logged at `error` level with `module: "app"`. Filter per-channel if some noise slips in.
72
+
73
+ ## Checking an error was captured in tests
74
+
75
+ Don't mock `process.on` — use a capturing channel and emit the listener directly:
76
+
77
+ ```ts
78
+ import { log, captureAnyUnhandledRejection, LogChannel } from "@warlock.js/logger";
79
+ import type { LoggingData } from "@warlock.js/logger";
80
+
81
+ class Capture extends LogChannel {
82
+ public name = "capture";
83
+ public received: LoggingData[] = [];
84
+ public log(data: LoggingData) { this.received.push({ ...data }); }
85
+ }
86
+
87
+ it("routes unhandled rejections to the logger", async () => {
88
+ const capture = new Capture();
89
+ const originalChannels = log.channels;
90
+ log.channels = [capture];
91
+
92
+ captureAnyUnhandledRejection();
93
+ process.emit("unhandledRejection", new Error("boom"), Promise.resolve());
94
+
95
+ await new Promise((r) => setTimeout(r, 0));
96
+
97
+ expect(capture.received[0]!.module).toBe("app");
98
+ expect(capture.received[0]!.action).toBe("unhandledRejection");
99
+
100
+ log.channels = originalChannels;
101
+ });
102
+ ```
103
+
104
+ ## Module + action the capture uses
105
+
106
+ Both listeners log with:
107
+ - `module: "app"`
108
+ - `action: "unhandledRejection"` or `action: "uncaughtException"`
109
+ - `message`: the rejection reason / exception (keep it as the raw `Error` object — file channels capture the stack).
110
+
111
+ If you want these routed to a specific file, filter on `data.module === "app"`. See [`@warlock.js/logger/filter-log-entries/SKILL.md`](@warlock.js/logger/filter-log-entries/SKILL.md).
112
+
113
+
114
+ ## configure-logger `@warlock.js/logger/configure-logger/SKILL.md`
115
+
116
+ ---
117
+ name: configure-logger
118
+ description: 'Register channels via log.addChannel / log.setChannels / log.configure({channels, autoFlushOn, redact, minLevel}) at boot. Triggers: `log.configure`, `log.addChannel`, `log.setChannels`, `Logger`, `autoFlushOn`, `disableAutoFlush`; "wire channels at startup", "branch logger by NODE_ENV", "isolate a library''s logger", "replace channel list"; typical import `import { log, Logger, ConsoleLog, FileLog } from "@warlock.js/logger"`. Skip: channel picks — `@warlock.js/logger/pick-log-channel/SKILL.md`; flushing — `@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`; redaction — `@warlock.js/logger/redact-sensitive-log-fields/SKILL.md`; competing libs `winston.createLogger`, `pino`.'
119
+ ---
120
+
121
+ # Setup — registering channels at startup
122
+
123
+ The logger is a singleton. Do all setup in one place, as early in the app entry point as possible.
124
+
125
+ ## The three channel-registration methods
126
+
127
+ | Method | Semantics |
128
+ |---|---|
129
+ | `log.addChannel(channel)` | **Appends.** Safe to call multiple times. |
130
+ | `log.setChannels([...])` | **Replaces** the full list. |
131
+ | `log.configure({ channels, autoFlushOn, redact, minLevel })` | **Replaces** channels if provided; installs auto-flush if provided; sets redact / minLevel if provided. All four are optional. |
132
+
133
+ All three return `this` — chainable.
134
+
135
+ ## Recommended pattern — one dedicated file
136
+
137
+ ```ts title="src/logger.ts"
138
+ import { log, ConsoleLog, FileLog, JSONFileLog } from "@warlock.js/logger";
139
+
140
+ if (process.env.NODE_ENV === "production") {
141
+ log.configure({
142
+ channels: [
143
+ new FileLog({ storagePath: "./storage/logs", chunk: "daily", rotate: true }),
144
+ new JSONFileLog({ storagePath: "./storage/logs-json", chunk: "daily" }),
145
+ ],
146
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
147
+ });
148
+ } else if (process.env.NODE_ENV === "test") {
149
+ log.setChannels([]); // silence logger during tests
150
+ } else {
151
+ log.setChannels([new ConsoleLog()]);
152
+ }
153
+ ```
154
+
155
+ Import it once at the top of `src/index.ts`:
156
+
157
+ ```ts title="src/index.ts"
158
+ import "./logger"; // side-effect: configures singleton
159
+ import { log } from "@warlock.js/logger";
160
+
161
+ log.info("app", "start", "Server listening on :3000");
162
+ ```
163
+
164
+ ## What `configure({ autoFlushOn })` does
165
+
166
+ Registers one process-level handler per event that calls `log.flushSync()` before Node exits. See [`@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md) for the full behavior table.
167
+
168
+ ```ts
169
+ log.configure({
170
+ channels: [new FileLog()],
171
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
172
+ });
173
+ // Now a buffered FileLog flushes on Ctrl+C, container stop, and natural exit.
174
+ ```
175
+
176
+ Calling `configure({ autoFlushOn })` a second time **replaces** previous handlers (not stacks them). Call `log.disableAutoFlush()` to tear them down.
177
+
178
+ ## Creating an isolated Logger
179
+
180
+ Rarely needed. Useful when a library wants its own channel list that doesn't share with the host app:
181
+
182
+ ```ts
183
+ import { Logger, ConsoleLog } from "@warlock.js/logger";
184
+
185
+ export const libraryLogger = new Logger();
186
+ libraryLogger.addChannel(new ConsoleLog({ filter: (d) => d.module === "my-lib" }));
187
+ ```
188
+
189
+ Every `new Logger()` gets a unique `id` (string, prefixed `"logger-"`).
190
+
191
+ ## Order matters — ANSI stripping across channels
192
+
193
+ `Logger.log` shallow-clones the entry per non-terminal channel before stripping ANSI codes. Registering a terminal channel (ConsoleLog) **after** a non-terminal one (FileLog) still works — ConsoleLog sees the original colored message. But if you register them in reverse and add a channel that mutates `data` in place, the non-terminal channel will see the terminal channel's version. Prefer the built-ins; custom channels should not mutate `data`.
194
+
195
+ ## When to call what
196
+
197
+ - **`addChannel`** — most common. Add channels as you discover you need them during setup.
198
+ - **`setChannels`** — when env branching makes the full list clear at once (production vs dev).
199
+ - **`configure`** — when you also want to install auto-flush, redact, or minLevel in the same call.
200
+
201
+ ## Combining everything
202
+
203
+ ```ts
204
+ log.configure({
205
+ channels: [
206
+ new ConsoleLog({ showContext: true }),
207
+ new FileLog({ chunk: "daily" }),
208
+ ],
209
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
210
+ redact: { paths: ["context.password", "context.headers.authorization"] },
211
+ minLevel: process.env.LOG_LEVEL === "debug" ? "debug" : "info",
212
+ });
213
+ ```
214
+
215
+ See [`@warlock.js/logger/redact-sensitive-log-fields/SKILL.md`](@warlock.js/logger/redact-sensitive-log-fields/SKILL.md) for the redact contract and [`@warlock.js/logger/filter-log-entries/SKILL.md`](@warlock.js/logger/filter-log-entries/SKILL.md) for `minLevel`.
216
+
217
+ ## See also
218
+
219
+ - [`@warlock.js/logger/pick-log-channel/SKILL.md`](@warlock.js/logger/pick-log-channel/SKILL.md) — what each built-in channel does
220
+ - [`@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md) — `autoFlushOn` event behavior
221
+
222
+
223
+ ## filter-log-entries `@warlock.js/logger/filter-log-entries/SKILL.md`
224
+
225
+ ---
226
+ name: filter-log-entries
227
+ description: 'Drop log entries — per-channel levels whitelist, per-channel filter predicate, logger-wide setMinLevel(level) fast path. Triggers: `levels`, `filter`, `minLevel`, `log.setMinLevel`, `shouldBeLogged`, `LoggingData`, `LogLevel`; "silence a noisy module", "route errors to a dedicated file", "raise global severity floor", "drop debug logs in prod"; typical import `import { log } from "@warlock.js/logger"`. Skip: custom sinks — `@warlock.js/logger/write-custom-log-channel/SKILL.md`; channel picks — `@warlock.js/logger/pick-log-channel/SKILL.md`; competing libs `pino.levels`, `winston.format.filter`, `debug` env var.'
228
+ ---
229
+
230
+ # Filtering — `levels` + `filter` predicate + `minLevel`
231
+
232
+ Every channel can silently drop entries it doesn't care about. Three mechanisms stack: a logger-wide `minLevel` floor (cheapest), then per-channel `levels` whitelist, then per-channel `filter` predicate.
233
+
234
+ ## 1. `levels` — the per-channel whitelist
235
+
236
+ ```ts
237
+ new FileLog({ levels: ["error", "warn"] });
238
+ // debug/info/success entries → skipped
239
+ // error/warn entries → written
240
+ ```
241
+
242
+ - Omitting `levels` (or passing `[]`) means **allow all five**.
243
+ - No regex / no range — it's a literal whitelist of `LogLevel` strings.
244
+
245
+ ## 2. `filter` — the per-channel custom predicate
246
+
247
+ ```ts
248
+ new ConsoleLog({
249
+ filter: (data) => data.module !== "healthcheck",
250
+ });
251
+ // Every entry is passed to the predicate; return false → skip.
252
+ ```
253
+
254
+ - `data` is the full `LoggingData`: `{ type, module, action, message, context? }`.
255
+ - Predicate runs **after** `levels` — an entry blocked by `levels` never reaches `filter`.
256
+
257
+ ## 3. `minLevel` — the logger-wide severity floor
258
+
259
+ For the common "drop everything below X" case, skip the per-channel `levels` array and use the logger-wide fast path:
260
+
261
+ ```ts
262
+ log.setMinLevel("info");
263
+ // debug entries are dropped before fan-out — no channel ever sees them.
264
+
265
+ log.configure({ minLevel: "warn" }); // shorthand inside configure()
266
+ ```
267
+
268
+ Severity ordering: `debug < info ≈ success < warn < error`. `success` is treated as informational severity — `setMinLevel("warn")` drops it.
269
+
270
+ Pass `undefined` to clear:
271
+
272
+ ```ts
273
+ log.setMinLevel(undefined); // accept everything again
274
+ ```
275
+
276
+ This runs **before** the channel loop — cheaper than per-channel `levels` filters when you want a uniform floor. Per-channel `levels` and `filter` still run on top for channels that need a tighter or differently-shaped rule.
277
+
278
+ ## Combining — real patterns
279
+
280
+ ### Route errors to a dedicated file
281
+
282
+ ```ts
283
+ log.setChannels([
284
+ new ConsoleLog(),
285
+ new FileLog({
286
+ name: "errors",
287
+ levels: ["error", "warn"],
288
+ chunk: "daily",
289
+ }),
290
+ ]);
291
+ // ConsoleLog sees everything; errors.log only grows with warnings and errors.
292
+ ```
293
+
294
+ ### Silence a noisy module
295
+
296
+ ```ts
297
+ new ConsoleLog({
298
+ filter: (data) => data.module !== "socket.io",
299
+ });
300
+ ```
301
+
302
+ ### Keep the dev terminal focused
303
+
304
+ ```ts
305
+ // Only surface the subsystem you're actively working on
306
+ new ConsoleLog({
307
+ filter: (data) => data.module === "auth",
308
+ });
309
+ ```
310
+
311
+ ### Errors always pass, info only for one module
312
+
313
+ ```ts
314
+ new ConsoleLog({
315
+ filter: (data) => data.type === "error" || data.module === "payments",
316
+ });
317
+ ```
318
+
319
+ ## Where filtering happens
320
+
321
+ `LogChannel.shouldBeLogged(data)` runs both checks in order:
322
+
323
+ ```ts
324
+ // levels check — fast path
325
+ if (this.config("levels")?.length && !this.config("levels").includes(data.type)) return false;
326
+
327
+ // filter predicate — only runs if levels allowed it
328
+ const filter = this.config("filter");
329
+ if (filter) return filter(data);
330
+
331
+ return true;
332
+ ```
333
+
334
+ If you extend `LogChannel` to write a custom channel, call `this.shouldBeLogged(data)` first thing inside your `log(data)` method — you inherit both mechanisms for free. See [`@warlock.js/logger/write-custom-log-channel/SKILL.md`](@warlock.js/logger/write-custom-log-channel/SKILL.md).
335
+
336
+ ## Logger-wide custom filtering — not a thing
337
+
338
+ There is no `logger.setGlobalFilter()`. Each channel filters itself. If you want the same predicate everywhere, pass it to every channel constructor (or wrap your channels in a helper).
339
+
340
+ ## Performance note
341
+
342
+ Filters run on **every** entry per channel. A synchronous, cheap predicate is fine. Avoid `await` inside — the channel receives a fully-formed `LoggingData` and the filter is sync-only (type: `(data: LoggingData) => boolean`).
343
+
344
+ The `minLevel` check is the fastest of the three (single comparison before fan-out), so prefer it when "drop everything below X uniformly" matches your need.
345
+
346
+
347
+ ## flush-logs-on-shutdown `@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`
348
+
349
+ ---
350
+ name: flush-logs-on-shutdown
351
+ description: 'Drain buffered channels before exit — log.flushSync() or log.configure({autoFlushOn: [''SIGINT'', ''SIGTERM'', ''beforeExit'']}) installs handlers that re-raise the signal. Triggers: `log.flushSync`, `autoFlushOn`, `enableAutoFlush`, `disableAutoFlush`, `SIGINT`, `SIGTERM`, `beforeExit`; "drain logs before exit", "wire SIGTERM for container shutdown", "my logs never showed after a crash", "graceful shutdown logging"; typical import `import { log, FileLog } from "@warlock.js/logger"`. Skip: error capture — `@warlock.js/logger/capture-unhandled-errors/SKILL.md`; custom sinks — `@warlock.js/logger/write-custom-log-channel/SKILL.md`; competing `pino.final`, `winston.end`; native `process.on(''exit'')`.'
352
+ ---
353
+
354
+ # Lifecycle — flushing buffered channels before exit
355
+
356
+ `FileLog` and `JSONFileLog` buffer entries in memory. A process that exits without draining loses the buffer.
357
+
358
+ ## The easy way — `autoFlushOn`
359
+
360
+ Tell the logger which process events should trigger a flush. It installs the handlers for you.
361
+
362
+ ```ts
363
+ log.configure({
364
+ channels: [new ConsoleLog(), new FileLog({ chunk: "daily" })],
365
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
366
+ });
367
+ ```
368
+
369
+ ### What each event does
370
+
371
+ | Event | Behavior |
372
+ |---|---|
373
+ | `SIGINT` / `SIGTERM` / `SIGHUP` / `SIGBREAK` / `SIGUSR2` | Flush → remove this handler → re-raise the signal so Node's default exit code runs (e.g. 130 for SIGINT). |
374
+ | `beforeExit` | Flush in place. Node continues its natural exit. |
375
+
376
+ ### Default recommendation
377
+
378
+ `["SIGINT", "SIGTERM", "beforeExit"]` covers:
379
+ - Local `Ctrl+C` (SIGINT)
380
+ - Container orchestrators (`docker stop`, Kubernetes sending SIGTERM)
381
+ - Natural exit (Node finished all work)
382
+
383
+ Add `"SIGHUP"` if you care about terminal disconnects. Add `"SIGUSR2"` if you use nodemon or pm2 restart.
384
+
385
+ ### Idempotency
386
+
387
+ Calling `enableAutoFlush` twice **replaces** previous handlers — it does not stack. `disableAutoFlush()` removes every handler this logger instance registered; safe to call when nothing is registered.
388
+
389
+ ## The manual way — your own handler
390
+
391
+ Use this when you need async work (close an HTTP server, drain a queue) **before** flushing:
392
+
393
+ ```ts
394
+ async function gracefulShutdown() {
395
+ await httpServer.close();
396
+ await queue.drain();
397
+ log.flushSync(); // still sync — guarantees disk write before exit
398
+ process.exit(0);
399
+ }
400
+
401
+ process.once("SIGINT", gracefulShutdown);
402
+ process.once("SIGTERM", gracefulShutdown);
403
+ ```
404
+
405
+ **If you go manual for a signal, skip it in `autoFlushOn`** — otherwise both handlers fire and ours re-raises the signal mid-way through your async work.
406
+
407
+ ## What `flushSync()` actually does
408
+
409
+ ```ts
410
+ log.flushSync();
411
+ // For every registered channel:
412
+ // if (channel.flushSync) channel.flushSync();
413
+ ```
414
+
415
+ - Synchronous I/O — blocks the event loop.
416
+ - Channels without `flushSync` (e.g. `ConsoleLog` — nothing to flush) are skipped silently.
417
+ - Works with and without `groupBy` on `FileLog` / `JSONFileLog`.
418
+ - No-op if every channel's buffer is empty.
419
+
420
+ `ConsoleLog` has no `flushSync` — it writes synchronously on every entry. `FileLog` and `JSONFileLog` both implement it.
421
+
422
+ ## Unhandled errors
423
+
424
+ If you use [`captureAnyUnhandledRejection()`](@warlock.js/logger/capture-unhandled-errors/SKILL.md), **include `"beforeExit"` in `autoFlushOn`**. Otherwise a crash logs the error into the buffer, then the process exits before the 5-second flush interval fires.
425
+
426
+ ```ts
427
+ log.configure({
428
+ channels: [new FileLog({ levels: ["error"] })],
429
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
430
+ });
431
+
432
+ captureAnyUnhandledRejection();
433
+ ```
434
+
435
+ ## What NOT to do
436
+
437
+ - **Don't `await` inside a signal handler you wrote yourself and then call `flushSync`** — if an async step rejects, you skip the flush. Wrap in `try { await x } finally { log.flushSync(); process.exit(1); }`.
438
+ - **Don't call `process.exit()` inside `autoFlushOn` handlers** — signal handlers here already re-raise the signal. Forcing an exit breaks exit codes.
439
+ - **Don't rely on the 5-second flush interval for shutdown safety.** It's a throughput optimization, not a durability guarantee.
440
+
441
+
442
+ ## logger-basics `@warlock.js/logger/logger-basics/SKILL.md`
443
+
444
+ ---
445
+ name: logger-basics
446
+ description: 'Start with @warlock.js/logger — the log singleton, five levels (debug / info / warn / error / success), channel fan-out, foundations. Triggers: `log`, `Logger`, `log.info`, `log.error`, `log.debug`, `log.warn`, `log.success`, `ConsoleLog`, `FileLog`, `JSONFileLog`; "how do I log in node", "warlock logger basics", "which logger skill do I need"; typical import `import { log, ConsoleLog, FileLog } from "@warlock.js/logger"`. Skip: channel picks — `@warlock.js/logger/pick-log-channel/SKILL.md`; setup — `@warlock.js/logger/configure-logger/SKILL.md`; competing libs `winston`, `pino`, `bunyan`, `log4js`, `signale`; native `console.log`.'
447
+ ---
448
+
449
+ # Log with channels
450
+
451
+ Multi-channel structured logger for Node.js. Three built-in channels (`ConsoleLog`, `FileLog`, `JSONFileLog`), an abstract `LogChannel` base for custom sinks, five severity levels, and a safe shutdown path via `Logger.enableAutoFlush(events)`.
452
+
453
+ > This skill is the logger **map** — read it first, then load the specific skill for the task.
454
+
455
+ ## Install
456
+
457
+ ```bash
458
+ yarn add @warlock.js/logger
459
+ ```
460
+
461
+ ## Foundations
462
+
463
+ The 11 things that are true in every logger use:
464
+
465
+ 1. **Public API is the `log` singleton** (`import { log } from "@warlock.js/logger"`). It's a `Logger` instance — call `log.info(...)`, `log.configure(...)`, etc. No callable `log(data)` form.
466
+ 2. **The singleton starts with zero channels.** Nothing is written until at least one channel is registered via `addChannel`, `setChannels`, or `configure`.
467
+ 3. **Custom instances:** `new Logger()` gives an isolated logger with the identical API. Almost always you want the singleton — reach for the class only when you need an isolated channel set (libraries, test sandboxes).
468
+ 4. **Five levels, closed union:** `"debug" | "info" | "warn" | "error" | "success"`. There are no custom levels today.
469
+ 5. **Channels can be filtered two ways:** a `levels` array (whitelist) and a `filter` predicate (custom logic). Both run on every entry. See [`@warlock.js/logger/filter-log-entries/SKILL.md`](@warlock.js/logger/filter-log-entries/SKILL.md).
470
+ 6. **Logger-wide minimum severity** is available via `log.setMinLevel("info")` (or `configure({ minLevel })`). Entries below the rank are dropped before fan-out — cheaper than per-channel filters.
471
+ 7. **Redaction** is two-layer additive: `configure({ redact })` sets the logger floor; `new XxxChannel({ redact: { paths: [...] } })` adds more paths on top. Channels can never remove paths from the logger floor. See [`@warlock.js/logger/redact-sensitive-log-fields/SKILL.md`](@warlock.js/logger/redact-sensitive-log-fields/SKILL.md).
472
+ 8. **`FileLog` and `JSONFileLog` buffer in memory.** They flush when `maxMessagesToWrite` (default `100`) is hit, when 5 seconds have elapsed since the last write, or when `flushSync()` is called. See [`@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md).
473
+ 9. **Non-terminal channels receive ANSI-stripped messages.** `Logger.log` shallow-clones the entry per non-terminal channel before stripping, so later terminal channels still get the colored original.
474
+ 10. **`JSONFileLog.extension` is always `"json"`.** The option is ignored for this channel.
475
+ 11. **`captureAnyUnhandledRejection()` registers process listeners.** Call it once at startup, after channels are registered. Calling it twice installs duplicate listeners. See [`@warlock.js/logger/capture-unhandled-errors/SKILL.md`](@warlock.js/logger/capture-unhandled-errors/SKILL.md).
476
+
477
+ ## Minimal startup example
478
+
479
+ ```ts
480
+ import { log, ConsoleLog, FileLog } from "@warlock.js/logger";
481
+
482
+ log.configure({
483
+ channels: [
484
+ new ConsoleLog(),
485
+ new FileLog({ chunk: "daily", storagePath: "./storage/logs" }),
486
+ ],
487
+ autoFlushOn: ["SIGINT", "SIGTERM", "beforeExit"],
488
+ });
489
+
490
+ await log.info("users", "register", "New user created");
491
+ await log.error("payments", "charge", new Error("Card declined"));
492
+ ```
493
+
494
+ ## The five levels
495
+
496
+ ```ts
497
+ log.debug("module", "action", "verbose detail"); // dev-only diagnostics
498
+ log.info("module", "action", "neutral event"); // user-visible event
499
+ log.warn("module", "action", "something off"); // recoverable concern
500
+ log.error("module", "action", error); // failure path
501
+ log.success("module", "action", "operation done"); // explicit success
502
+ ```
503
+
504
+ Every call signature is the same — `module`, `action`, `message`, optional `context`. `message` can be a string, object, or `Error` instance (file channels capture the stack).
505
+
506
+ ## Pick a skill
507
+
508
+ | If the task is about… | Load |
509
+ | --- | --- |
510
+ | Picking a channel — what each built-in does, when to use which | [`@warlock.js/logger/pick-log-channel/SKILL.md`](@warlock.js/logger/pick-log-channel/SKILL.md) |
511
+ | Startup — registering channels, environment-based setup, the `configure` method | [`@warlock.js/logger/configure-logger/SKILL.md`](@warlock.js/logger/configure-logger/SKILL.md) |
512
+ | Filtering log output (`levels`, `filter`, per-channel routing, `minLevel`) | [`@warlock.js/logger/filter-log-entries/SKILL.md`](@warlock.js/logger/filter-log-entries/SKILL.md) |
513
+ | Graceful shutdown — `flushSync`, `autoFlushOn`, signal behavior | [`@warlock.js/logger/flush-logs-on-shutdown/SKILL.md`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md) |
514
+ | Extending `LogChannel` to build a custom sink (Slack, database, HTTP) | [`@warlock.js/logger/write-custom-log-channel/SKILL.md`](@warlock.js/logger/write-custom-log-channel/SKILL.md) |
515
+ | Routing Node's `unhandledRejection` / `uncaughtException` through the logger | [`@warlock.js/logger/capture-unhandled-errors/SKILL.md`](@warlock.js/logger/capture-unhandled-errors/SKILL.md) |
516
+ | `log.assert(...)` and `log.timer(...)` shorthand helpers | [`@warlock.js/logger/use-log-helpers/SKILL.md`](@warlock.js/logger/use-log-helpers/SKILL.md) |
517
+ | Redacting secrets — logger floor + additive channel paths | [`@warlock.js/logger/redact-sensitive-log-fields/SKILL.md`](@warlock.js/logger/redact-sensitive-log-fields/SKILL.md) |
518
+ | Tests that assert on log output, or code under test that logs | [`@warlock.js/logger/test-logging-code/SKILL.md`](@warlock.js/logger/test-logging-code/SKILL.md) |
519
+
520
+ ## Things NOT to do
521
+
522
+ - Don't try `log(module, action, message)` or `log({...})` directly — `log` is a `Logger` instance, not a function. Use `log.info(...)`, `log.error(...)`, etc., or the explicit `log.log({ type, module, action, message })` for the data-object form.
523
+ - Don't set `extension` on `JSONFileLog` — it's hardcoded to `"json"` and your value is silently ignored.
524
+ - Don't register multiple `FileLog` instances with the same `name` in the same `storagePath` — the lookup via `log.channel("file")` returns only one, and they'll fight over the same file.
525
+ - Don't mix `autoFlushOn: ["SIGINT"]` with your own `process.on("SIGINT", ...)` handler — both fire, and ours re-raises mid-way through your async work.
526
+ - Don't `await log.info(...)` expecting the write to be on disk — `FileLog` buffers. Call `log.flushSync()` (or rely on `autoFlushOn`) before the process exits.
527
+ - Don't call `captureAnyUnhandledRejection()` more than once — it re-registers listeners every call and your rejections get logged N times.
528
+ - Don't shadow the import in local code: `for (const log of logEntries) { ... }` will hide the singleton inside that block. Rename loop variables (`entry`, `record`) when working with logger imports.
529
+
530
+
531
+ ## overview `@warlock.js/logger/overview/SKILL.md`
532
+
533
+ ---
534
+ name: overview
535
+ description: 'Front-door orientation for `@warlock.js/logger` — structured channel-based logging with five severity levels, PII redaction floor, buffered file/JSON channels, signal-flush on shutdown, ergonomic helpers (timer, assert). Standalone — no `@warlock.js/core` required. TRIGGER when: code imports anything from `@warlock.js/logger`; user asks "what does @warlock.js/logger do", "compare with pino / winston / bunyan", "structured logging for Node", "which logger should I use", "how do channels work"; package.json adds `@warlock.js/logger`. Skip: specific task already known — load the matching task skill directly (`logger-basics`, `configure-logger`, `pick-log-channel`, `write-custom-log-channel`, `redact-sensitive-log-fields`, `filter-log-entries`, `flush-logs-on-shutdown`, `capture-unhandled-errors`, `use-log-helpers`, `test-logging-code`); plain `console.log` in throwaway scripts.'
536
+ ---
537
+
538
+ # `@warlock.js/logger` — overview
539
+
540
+ Structured logging for Node. Five severity levels, a singleton plus a `Logger` class, channel-based fan-out (one entry → many sinks), PII redaction as a floor that channels can extend, buffered file writes with signal-triggered flush on shutdown, and a couple of ergonomic helpers (`timer`, `assert`) that turn boilerplate into one-liners.
541
+
542
+ Ships standalone — `@warlock.js/core` is not required. Drop it into any Node project.
543
+
544
+ ## When to reach for it
545
+
546
+ - Building a Node service that needs **structured** logs (key-value pairs, not bare strings) and you want them to land in multiple destinations (console for dev, JSON file for prod, third-party sink for audits) without rewriting the call sites.
547
+ - You'd reach for **pino** or **winston** but want a smaller surface that's already wired into Warlock conventions (`module / action / message` shape, redaction floor, signal flush built-in).
548
+ - Your team agrees that **`console.log` doesn't survive contact with production** — you need filtering, level routing, channel-specific sinks, and a redaction story before secrets leak into Slack/Datadog.
549
+
550
+ Skip if your code is a throwaway script where `console.log` is genuinely fine — there's no value in adding a dependency for one-off logs.
551
+
552
+ ## The mental model in one paragraph
553
+
554
+ You write `log.info("auth", "login", "user signed in", { userId })`. The logger fans that single entry out to every registered channel (`ConsoleLog`, `FileLog`, `JSONFileLog`, or your custom subclass). Each channel decides whether to emit it (per-level whitelist, per-channel filter predicate, logger-wide minimum severity). Redaction runs once at the logger level and can be extended per channel — never relaxed. Buffered channels (file + JSON file) drain on flush, either manually (`log.flushSync()`) or automatically via signal handlers (`enableAutoFlush(['SIGINT', 'SIGTERM', 'beforeExit'])`). That's the whole package.
555
+
556
+ ## Skills index
557
+
558
+ Ten task skills cover everything. Load the one that matches your job — most callers only ever need `logger-basics` + `configure-logger` + `pick-log-channel`.
559
+
560
+ ### Foundations
561
+
562
+ #### [`logger-basics`](@warlock.js/logger/logger-basics/SKILL.md)
563
+ Start here. The `log` singleton, the five levels (`debug` / `info` / `warn` / `error` / `success`), how fan-out works, the `module / action / message / context` shape every entry carries.
564
+
565
+ #### [`configure-logger`](@warlock.js/logger/configure-logger/SKILL.md)
566
+ Wire channels at boot — `log.addChannel`, `log.setChannels`, `log.configure({ channels, autoFlushOn, redact, minLevel })`. Branch on `NODE_ENV`, replace the channel list, isolate a library's logger from the host singleton.
567
+
568
+ ### Channels
569
+
570
+ #### [`pick-log-channel`](@warlock.js/logger/pick-log-channel/SKILL.md)
571
+ Pick one of the three built-ins: `ConsoleLog` (terminal, colored), `FileLog` (plain `.log` on disk with rotation), `JSONFileLog` (structured JSON for aggregators — Datadog, Loki, ELK).
572
+
573
+ #### [`write-custom-log-channel`](@warlock.js/logger/write-custom-log-channel/SKILL.md)
574
+ Extend `LogChannel<Options>` for sinks the built-ins don't cover — Slack, HTTP endpoint, in-memory buffer, database. The lazy `init()` lifecycle (`setTimeout(0)`) and the `terminal: true/false` ANSI-stripping behavior are subtle — read this skill before subclassing.
575
+
576
+ ### Production concerns
577
+
578
+ #### [`redact-sensitive-log-fields`](@warlock.js/logger/redact-sensitive-log-fields/SKILL.md)
579
+ Strip secrets before they reach a sink. Logger-wide `setRedact({ paths, censor })` is the security floor; per-channel `redact` configs add paths (never remove). Dotted-glob paths (`*`, `**`); censor as string or function `(value, path) => any`.
580
+
581
+ #### [`filter-log-entries`](@warlock.js/logger/filter-log-entries/SKILL.md)
582
+ Drop entries before they cost anything. Logger-wide `setMinLevel("info")` is the fast path; per-channel `levels` array + `filter` predicate for fine control.
583
+
584
+ #### [`flush-logs-on-shutdown`](@warlock.js/logger/flush-logs-on-shutdown/SKILL.md)
585
+ Buffered channels (file + JSON file) need explicit drain. `log.flushSync()` manually, or `enableAutoFlush(['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGBREAK', 'SIGUSR2', 'beforeExit'])` to wire process signals. Signals are re-raised after flush so Node's default exit still runs.
586
+
587
+ #### [`capture-unhandled-errors`](@warlock.js/logger/capture-unhandled-errors/SKILL.md)
588
+ `captureAnyUnhandledRejection()` hooks `unhandledRejection` + `uncaughtException` and routes both through `log.error("app", ...)`. One call at startup, pair with `autoFlushOn: ['beforeExit']` to land the entry on disk.
589
+
590
+ ### Ergonomics + testing
591
+
592
+ #### [`use-log-helpers`](@warlock.js/logger/use-log-helpers/SKILL.md)
593
+ Two shortcuts every `Logger` exposes: `log.assert(condition, module, action, message, context?)` logs an error only when the condition is falsy (free on the happy path); `log.timer(module, action)` returns an end-function that emits `info` with a measured `durationMs`.
594
+
595
+ #### [`test-logging-code`](@warlock.js/logger/test-logging-code/SKILL.md)
596
+ Silence the logger globally in tests via `log.setChannels([])` in `setupFiles`. Assert specific entries with a capturing `LogChannel` subclass — it proves an entry was actually delivered through the pipeline (filters, redaction), not merely that a method was called, and it isolates cleanly by swapping `log.channels`.
597
+
598
+ ## Built-in channels at a glance
599
+
600
+ | Channel | Sink | `terminal` | Buffered |
601
+ | --- | --- | --- | --- |
602
+ | `ConsoleLog` | `process.stdout` | `true` (colors kept) | no |
603
+ | `FileLog` | `.log` files | `false` (ANSI stripped) | yes (5s timer or 100-entry buffer) |
604
+ | `JSONFileLog` | `.json` files | `false` (ANSI stripped) | yes (same buffering) |
605
+
606
+ `terminal: true` is the flag that decides whether the channel sees raw colored messages or stripped plain text. Custom channels: pick `true` if you write to a terminal, `false` for anything else.
607
+
608
+ ## What this package deliberately doesn't do
609
+
610
+ - **Distributed tracing.** Use OpenTelemetry. Logger gets you structured local logs with `module / action`; trace correlation is a different problem.
611
+ - **Log shipping.** Write a custom channel that POSTs to your aggregator, or use `JSONFileLog` + a sidecar (fluentbit, vector, promtail). The package doesn't bundle network sinks.
612
+ - **Pretty-printing of arbitrary objects.** `ConsoleLog` has a `showContext` flag that runs `util.inspect` on the context object; for richer formatting, use `JSONFileLog` and view the file through your favorite viewer.
613
+ - **Log analysis.** Querying / aggregating / alerting is on the sink side (Loki, ELK, Datadog).
614
+
615
+ ## See also
616
+
617
+ - [`@warlock.js/core/warlock-conventions`](@warlock.js/core/warlock-conventions/SKILL.md) — the parent framework's conventions; logger is one of its foundation packages and ships transitively when you install core.
618
+ - When synced via agent-kit, this `overview/SKILL.md` is flattened to the front-door skill `.claude/skills/warlock-js-logger-overview/` — every cross-link above uses the `@warlock.js/logger/<skill>/SKILL.md` name form so it survives that flattening.
619
+
620
+
621
+ ## pick-log-channel `@warlock.js/logger/pick-log-channel/SKILL.md`
622
+
623
+ ---
624
+ name: pick-log-channel
625
+ description: 'Pick one of the three built-in channels — ConsoleLog (terminal), FileLog (plain text on disk), JSONFileLog (structured JSON for aggregators like Loki / Datadog / Elastic). Triggers: `ConsoleLog`, `FileLog`, `JSONFileLog`, `chunk`, `rotate`, `groupBy`, `maxFileSize`, `showContext`, `log.channel`; "log to a file", "rotate log files", "daily log chunks", "json logs for datadog / loki / elastic"; typical import `import { ConsoleLog, FileLog, JSONFileLog } from "@warlock.js/logger"`. Skip: custom sinks — `@warlock.js/logger/write-custom-log-channel/SKILL.md`; registration — `@warlock.js/logger/configure-logger/SKILL.md`; competing libs `winston-daily-rotate-file`, `pino-pretty`.'
626
+ ---
627
+
628
+ # Channels — which one to pick and how to configure it
629
+
630
+ Three built-in channels. A channel is a destination for a log entry — the logger fans out every entry to every registered channel in parallel.
631
+
632
+ ## The decision
633
+
634
+ | Need | Pick |
635
+ |---|---|
636
+ | Local dev, colored output in the terminal | `ConsoleLog` |
637
+ | Plain text `.log` files on disk — humans read them | `FileLog` |
638
+ | Structured `.json` files — a log aggregator (Loki / Datadog / Elastic) reads them | `JSONFileLog` |
639
+
640
+ Most production setups use **two** channels: `ConsoleLog` + one file channel. Dev uses `ConsoleLog` only.
641
+
642
+ ## `ConsoleLog`
643
+
644
+ Zero config. Colored, icon-prefixed lines to the terminal.
645
+
646
+ ```ts
647
+ import { ConsoleLog } from "@warlock.js/logger";
648
+
649
+ new ConsoleLog();
650
+ // ⚙ (2024-03-15T10:22:00.000Z) [auth] [hashPassword] Hashing started
651
+ // ℹ (2024-03-15T10:22:01.482Z) [users] [register] New user created
652
+ // ✗ (2024-03-15T10:22:03.111Z) [payments] [charge] Card declined
653
+ ```
654
+
655
+ Properties:
656
+ - `name = "console"`, `terminal = true`
657
+ - Accepts `ConsoleLogConfig` — `levels`, `filter`, `dateFormat`, `showContext`, `contextDepth`
658
+ - If `message` is an object, a second `console.log(message)` is issued so Node's inspector can expand it
659
+
660
+ ### Showing context
661
+
662
+ By default `ConsoleLog` drops the `context` payload (the file/JSON channels still keep it). Flip `showContext: true` to render it on a second line — useful in development:
663
+
664
+ ```ts
665
+ new ConsoleLog({ showContext: true });
666
+
667
+ log.info("payments", "charge", "card declined", { userId: 42, amount: 1999 });
668
+ // ℹ (…) [payments] [charge] card declined
669
+ // ↳ { userId: 42, amount: 1999 }
670
+ ```
671
+
672
+ Tune `contextDepth` (default `4`) to clamp how deep `util.inspect` recurses into nested objects.
673
+
674
+ ## `FileLog`
675
+
676
+ Plain text. Buffers in memory, flushes to disk periodically.
677
+
678
+ ```ts
679
+ import { FileLog } from "@warlock.js/logger";
680
+
681
+ new FileLog({
682
+ storagePath: "./storage/logs", // default: process.cwd() + "/storage/logs"
683
+ name: "app", // default: "app"
684
+ extension: "log", // default: "log"
685
+ chunk: "daily", // "single" (default) | "daily" | "hourly"
686
+ rotate: true, // default: true
687
+ maxFileSize: 10 * 1024 * 1024, // default: 10MB — triggers rotation
688
+ maxMessagesToWrite: 100, // default: 100 — flush threshold
689
+ groupBy: ["level", "module"], // optional subdirectory nesting
690
+ });
691
+ ```
692
+
693
+ Line format: `[date time] [level] [module][action]: message` — or a `[trace]` block when `message` is an `Error`.
694
+
695
+ ### Key gotchas
696
+
697
+ - **Buffers!** Messages sit in memory until either `maxMessagesToWrite` is reached, 5 seconds pass, or `flushSync()` is called. A process that crashes without flushing loses buffered entries.
698
+ - **`chunk: "daily"` picks a filename per day.** File name becomes `DD-MM-YYYY.log`. Combined with `rotate: true`, rotated archives get `Date.now()` suffixed.
699
+ - **`groupBy` nests directories.** `groupBy: ["level", "module"]` produces `storage/logs/error/payments/app.log`. Order matters.
700
+ - **Dispose channels you discard.** A live `FileLog` keeps a 5-second flush interval running. If you swap the channel list at runtime (reconfigure the logger), call `channel.dispose()` on the old instance — it clears that timer and drains the buffer one last time. Skipping it leaks one timer per discarded channel and keeps the event loop alive. (Channels that live for the whole process don't need this — process exit clears the timer.)
701
+
702
+ ## `JSONFileLog`
703
+
704
+ Subclass of `FileLog` — same buffering, chunking, rotation, grouping. Output is a JSON object with a `messages` array:
705
+
706
+ ```json
707
+ {
708
+ "messages": [
709
+ {
710
+ "content": "Card declined",
711
+ "level": "error",
712
+ "date": "15-03-2024 10:22:03",
713
+ "module": "payments",
714
+ "action": "charge",
715
+ "stack": [
716
+ "Error: Card declined",
717
+ " at chargeCard (/app/src/payments.ts:42:11)"
718
+ ]
719
+ }
720
+ ]
721
+ }
722
+ ```
723
+
724
+ Differences from `FileLog`:
725
+ - `name = "fileJson"` (**not** `"json"` — use this exact string for `log.channel("fileJson")`)
726
+ - `extension` is always `"json"` — the option is silently ignored
727
+ - Error `stack` is stored as `string[]` (split on newlines) — easy to query in aggregators
728
+ - `content` holds the original user-supplied `message` (not a pre-formatted line)
729
+ - Corrupted existing file → reinitialized to `{ messages: [] }` on next write (does not throw)
730
+ - **Safe serialization by construction.** All writes go through `safe-stable-stringify` with a custom `Error` replacer — circular refs become `"[Circular]"`, BigInt is stringified, functions/symbols are dropped, nested `Error` instances expand to `{ name, message, stack, ...enumerable }`. A context payload with a class graph or circular reference will never throw during the write.
731
+
732
+ ## Shared config — `BasicLogConfigurations`
733
+
734
+ Every channel constructor accepts at minimum:
735
+
736
+ ```ts
737
+ type BasicLogConfigurations = {
738
+ levels?: LogLevel[]; // whitelist — omit or [] to allow all
739
+ filter?: (data: LoggingData) => boolean; // custom predicate
740
+ dateFormat?: { date?: string; time?: string }; // Day.js format strings
741
+ context?: (data) => Promise<Record<string, any>>; // reserved — not yet read
742
+ };
743
+ ```
744
+
745
+ Concrete file channels extend this with their storage/chunk/rotate/groupBy options via intersection.
746
+
747
+ ## Picking a channel by name at runtime
748
+
749
+ ```ts
750
+ log.channel("console"); // → ConsoleLog | undefined
751
+ log.channel("file"); // → FileLog | undefined
752
+ log.channel("fileJson"); // ← note the name — NOT "json"
753
+ ```
754
+
755
+ If two channels share a `name`, only one is reachable this way — the search returns the first match.
756
+
757
+ ## See also
758
+
759
+ - [`@warlock.js/logger/configure-logger/SKILL.md`](@warlock.js/logger/configure-logger/SKILL.md) — registering channels at startup
760
+ - [`@warlock.js/logger/filter-log-entries/SKILL.md`](@warlock.js/logger/filter-log-entries/SKILL.md) — `levels` and `filter` config in detail
761
+ - [`@warlock.js/logger/write-custom-log-channel/SKILL.md`](@warlock.js/logger/write-custom-log-channel/SKILL.md) — extending `LogChannel` for custom sinks
762
+
763
+
764
+ ## redact-sensitive-log-fields `@warlock.js/logger/redact-sensitive-log-fields/SKILL.md`
765
+
766
+ ---
767
+ name: redact-sensitive-log-fields
768
+ description: 'Strip secrets from log output — two-layer additive redaction via log.configure({redact: {paths}}) (logger floor) + per-channel redact (more paths on top). Dotted glob paths (*, **). Triggers: `redact`, `paths`, `censor`, `log.setRedact`, `applyRedact`; "redact passwords in logs", "strip tokens from log output", "hide authorization headers", "scrub PII before logging"; typical import `import { log } from "@warlock.js/logger"`. Skip: filtering — `@warlock.js/logger/filter-log-entries/SKILL.md`; custom sinks — `@warlock.js/logger/write-custom-log-channel/SKILL.md`; competing libs `pino.redact`, `fast-redact`.'
769
+ ---
770
+
771
+ # Redaction — keeping secrets out of logs
772
+
773
+ Two layers, both opt-in. Configured at the logger and/or per channel.
774
+
775
+ ## The model in one line
776
+
777
+ > Logger-wide redaction is the security floor. Per-channel redaction adds more paths. **No channel can ever undo a logger-wide redaction.**
778
+
779
+ That guarantee is the whole point — once you've set `password` to redact at the logger, you can audit one place to know nothing leaks it, regardless of how many channels you add.
780
+
781
+ ## Logger-wide floor
782
+
783
+ ```ts
784
+ import { log } from "@warlock.js/logger";
785
+
786
+ log.configure({
787
+ redact: {
788
+ paths: [
789
+ "context.password",
790
+ "context.*.token",
791
+ "context.headers.authorization",
792
+ ],
793
+ censor: "[REDACTED]", // default — string or function
794
+ },
795
+ });
796
+
797
+ // runtime equivalent:
798
+ log.setRedact({ paths: ["context.password"] });
799
+ log.setRedact(undefined); // clear
800
+ ```
801
+
802
+ Every channel sees the redacted entry. Cheap: applied **once** before fan-out; channels share the redacted clone unless they add their own paths.
803
+
804
+ ## Per-channel additive
805
+
806
+ ```ts
807
+ new SlackChannel({
808
+ webhook: "...",
809
+ redact: {
810
+ paths: ["context.user.email", "context.metadata.*"],
811
+ // censor inherited from logger-wide when omitted
812
+ },
813
+ });
814
+ ```
815
+
816
+ The channel's `paths` are **merged** with the logger floor — the channel runs a single combined redact pass, never replaces the floor. The channel's `censor` (if provided) wins for both its own and the logger's paths in this channel only; the logger floor still uses its own censor for other channels.
817
+
818
+ ### When to set redact per-channel
819
+
820
+ - Loud destinations with broader audiences (Slack, Discord, error trackers, anything off your machine) — redact more aggressively.
821
+ - Local-only destinations (FileLog you alone read, the dev terminal) — keep the floor minimal so you can debug.
822
+
823
+ ### When NOT to set it
824
+
825
+ If you want raw context in your dev terminal, **don't add redact at the logger level** — set it only on the file/JSON/network channels. Logger-wide is the floor, so it applies everywhere; you can't opt a single channel out.
826
+
827
+ ## Path syntax
828
+
829
+ Paths are dotted glob patterns evaluated against the full `LoggingData`:
830
+
831
+ ```
832
+ type LoggingData = {
833
+ type: "info" | ...,
834
+ module: string,
835
+ action: string,
836
+ message: any, // ← prefix paths with "message." to redact here
837
+ context?: object, // ← prefix paths with "context." to redact here
838
+ };
839
+ ```
840
+
841
+ | Pattern | Matches |
842
+ | --- | --- |
843
+ | `context.password` | exactly `data.context.password` |
844
+ | `context.*.token` | `data.context.<any>.token` (one segment in between) |
845
+ | `**.password` | `data.context.password`, `data.context.user.password`, … any depth |
846
+ | `message.apiKey` | when message is an object, `data.message.apiKey` |
847
+ | `context.users.*.token` | array element redaction (`*` matches indices too) |
848
+
849
+ Wildcards:
850
+
851
+ - `*` — exactly one segment (any object key, any array index).
852
+ - `**` — zero or more segments, greedily; matches at any depth.
853
+
854
+ ## Censor variants
855
+
856
+ ```ts
857
+ // String — replace with a literal.
858
+ { censor: "[REDACTED]" }
859
+ { censor: "***" }
860
+
861
+ // Function — receives original value + dotted path, returns the replacement.
862
+ {
863
+ censor: (value, path) => {
864
+ if (typeof value !== "string") return "[REDACTED]";
865
+ return value.length > 4 ? `${value.slice(0, 2)}***${value.slice(-2)}` : "***";
866
+ },
867
+ }
868
+ ```
869
+
870
+ Function censors are called for every match — keep them cheap. The path is the actual matched location (e.g. `"context.users.0.token"` for an array hit).
871
+
872
+ ## Immutability
873
+
874
+ `applyRedact` always returns a deep clone — your input data is never mutated. `Date` and `Error` instances are reconstructed (so `instanceof` checks still work). Circular references are tolerated.
875
+
876
+ ## What about the `message` field?
877
+
878
+ If `message` is a plain object, paths under `message.*` work as expected. If `message` is a string (the most common case), redaction won't scan it — string scrubbing requires regex and is out of scope for this primitive. Wrap secrets in `context` and they'll be redacted reliably.
879
+
880
+ ## Performance notes
881
+
882
+ - **No redact configured** → zero overhead (no clone, no walk).
883
+ - **Logger-wide redact only** → one deep clone + one path-walk per `log()` call, shared by every channel.
884
+ - **Channel adds paths** → that channel re-clones from the original input and runs the merged pass once. Other channels still share the cheaper logger-wide clone.
885
+ - Each path is matched independently; cost grows linearly with `paths.length`.
886
+
887
+ For most apps with `<10` redact paths and shallow context, the cost is below 100µs per entry. If you're logging millions of entries per second through paths like `**.something`, profile before scaling up — `**` is the only pattern that recurses through every key.
888
+
889
+
890
+ ## test-logging-code `@warlock.js/logger/test-logging-code/SKILL.md`
891
+
892
+ ---
893
+ name: test-logging-code
894
+ description: 'Test code that touches the logger — silence globally via log.setChannels([]) in setupFiles, assert specific log lines via a capturing LogChannel subclass (prefer it over vi.spyOn — it asserts on delivered entries, not just method calls, and isolates the shared singleton cleanly). Triggers: `log.setChannels`, `LogChannel`, `LoggingData`, `Logger`, `log.channels`; "silence logger in vitest", "assert a log line was emitted", "capture log output in tests", "test code that logs"; typical import `import { log, Logger, LogChannel, type LoggingData } from "@warlock.js/logger"`. Skip: custom sinks — `@warlock.js/logger/write-custom-log-channel/SKILL.md`; filtering — `@warlock.js/logger/filter-log-entries/SKILL.md`; competing `vi.spyOn(console)`, `jest.spyOn`.'
895
+ ---
896
+
897
+ # Testing — code that logs, and asserting on log output
898
+
899
+ Two scenarios: **silencing the logger during tests** (most common) and **asserting that a specific log line was emitted**.
900
+
901
+ ## Silence the logger during tests
902
+
903
+ Clear every channel once, globally. No output, no file handles, no noise.
904
+
905
+ ```ts title="src/setupTests.ts"
906
+ import { log } from "@warlock.js/logger";
907
+
908
+ log.setChannels([]);
909
+ ```
910
+
911
+ Wire it in Vitest:
912
+
913
+ ```ts title="vitest.config.ts"
914
+ import { defineConfig } from "vitest/config";
915
+
916
+ export default defineConfig({
917
+ test: {
918
+ setupFiles: ["src/setupTests.ts"],
919
+ },
920
+ });
921
+ ```
922
+
923
+ ## Assert on log output — use a capturing channel
924
+
925
+ Don't spy on `console.log` and don't mock `log.info` — assert on what a channel actually received instead (see "Why not spy on `log.info`?" below). The cleanest pattern is a tiny channel that records what it sees:
926
+
927
+ ```ts
928
+ import { LogChannel } from "@warlock.js/logger";
929
+ import type { LoggingData } from "@warlock.js/logger";
930
+
931
+ class CapturingChannel extends LogChannel {
932
+ public name = "capture";
933
+ public terminal = false;
934
+ public received: LoggingData[] = [];
935
+ public log(data: LoggingData) { this.received.push({ ...data }); }
936
+ }
937
+ ```
938
+
939
+ ### Test against the singleton
940
+
941
+ ```ts
942
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
943
+ import { log } from "@warlock.js/logger";
944
+ import { createUser } from "./users";
945
+
946
+ describe("createUser", () => {
947
+ let capture: CapturingChannel;
948
+ let originalChannels: typeof log.channels;
949
+
950
+ beforeEach(() => {
951
+ capture = new CapturingChannel();
952
+ originalChannels = log.channels;
953
+ log.channels = [capture];
954
+ });
955
+
956
+ afterEach(() => {
957
+ log.channels = originalChannels;
958
+ });
959
+
960
+ it("logs a success entry when the user is created", async () => {
961
+ await createUser({ email: "a@b.com" });
962
+
963
+ expect(capture.received).toContainEqual(
964
+ expect.objectContaining({
965
+ type: "success",
966
+ module: "users",
967
+ action: "create",
968
+ }),
969
+ );
970
+ });
971
+ });
972
+ ```
973
+
974
+ ### Test an isolated logger (avoid touching the singleton)
975
+
976
+ If the code under test accepts a logger via injection, create one per test:
977
+
978
+ ```ts
979
+ import { Logger } from "@warlock.js/logger";
980
+
981
+ const testLogger = new Logger();
982
+ const capture = new CapturingChannel();
983
+ testLogger.addChannel(capture);
984
+
985
+ await createUser({ email: "a@b.com" }, testLogger);
986
+
987
+ expect(capture.received[0]!.type).toBe("success");
988
+ ```
989
+
990
+ No cleanup needed — the local `Logger` is garbage-collected.
991
+
992
+ ## Why not spy on `log.info`?
993
+
994
+ `log` is a plain `Logger` instance (`export const log = new Logger()`) and every level method lives on the prototype, so `vi.spyOn(log, "info")` *does* technically work. Prefer the capturing channel anyway:
995
+
996
+ - A spy on `log.info` proves the method was **called**, not that an entry was **delivered** — it skips the whole pipeline (`minLevel` floor, redaction, per-channel `levels` / `filter`). A capturing channel asserts on the entry your code under test actually produced after all of that ran.
997
+ - The `log` singleton is shared global state. A spy you forget to `mockRestore()` leaks into the next test; swapping `log.channels` and restoring it in `afterEach` is the same amount of code and isolates cleanly.
998
+ - Code that logs through `log.error(...)` and the bare object form `log.log({ type, ... })` both land in channels, but only the level shortcut goes through `log.info` — a channel catches both.
999
+
1000
+ So capture through a channel as shown above; reach for a method spy only when you specifically want to assert "this exact shortcut was invoked".
1001
+
1002
+ ## Testing a custom channel
1003
+
1004
+ Write specs against the channel directly; don't route through `Logger`:
1005
+
1006
+ ```ts
1007
+ import { describe, it, expect, vi } from "vitest";
1008
+ import { SlackLog } from "./slack-log";
1009
+
1010
+ describe("SlackLog", () => {
1011
+ it("skips non-error levels", async () => {
1012
+ const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response());
1013
+
1014
+ const channel = new SlackLog({ webhookUrl: "https://test", levels: ["error"] });
1015
+ await channel.log({ type: "info", module: "x", action: "y", message: "z" });
1016
+
1017
+ expect(fetchSpy).not.toHaveBeenCalled();
1018
+ });
1019
+ });
1020
+ ```
1021
+
1022
+ ## Testing `FileLog` and `JSONFileLog`
1023
+
1024
+ Use real temp directories — it's the only way to exercise file IO, rotation, chunking, and JSON I/O with fidelity:
1025
+
1026
+ ```ts
1027
+ import fs from "fs";
1028
+ import os from "os";
1029
+ import path from "path";
1030
+ import { randomUUID } from "node:crypto";
1031
+
1032
+ function tempDir() {
1033
+ const dir = path.join(os.tmpdir(), "logger-test", randomUUID());
1034
+ fs.mkdirSync(dir, { recursive: true });
1035
+ return dir;
1036
+ }
1037
+ ```
1038
+
1039
+ Clean up in `afterEach(() => fs.rmSync(dir, { recursive: true, force: true }))`.
1040
+
1041
+ ## Waiting for async init
1042
+
1043
+ `LogChannel.init()` runs inside a `setTimeout(0)`. Before asserting on post-init behavior, yield once:
1044
+
1045
+ ```ts
1046
+ const channel = new FileLog({ storagePath: tempDir() });
1047
+ await new Promise((r) => setTimeout(r, 10));
1048
+ // Now `channel.isInitialized` is true and it's safe to call `channel.log(...)` for real I/O.
1049
+ ```
1050
+
1051
+ ## Testing `captureAnyUnhandledRejection`
1052
+
1053
+ Don't actually throw unhandled rejections in tests — emit the listener directly:
1054
+
1055
+ ```ts
1056
+ captureAnyUnhandledRejection();
1057
+ process.emit("unhandledRejection", new Error("test"), Promise.resolve());
1058
+ ```
1059
+
1060
+ See [`@warlock.js/logger/capture-unhandled-errors/SKILL.md`](@warlock.js/logger/capture-unhandled-errors/SKILL.md) for a full example.
1061
+
1062
+
1063
+ ## use-log-helpers `@warlock.js/logger/use-log-helpers/SKILL.md`
1064
+
1065
+ ---
1066
+ name: use-log-helpers
1067
+ description: 'Two DX shortcuts on every Logger — log.assert(condition, module, action, message, context?) logs an error when condition is falsy (free on the happy path), log.timer(module, action) returns an end-function emitting an info entry with measured duration. Triggers: `log.assert`, `log.timer`, `durationMs`; "assert an invariant via logger", "measure how long an operation took", "time a request", "log operation duration"; typical import `import { log } from "@warlock.js/logger"`. Skip: basics — `@warlock.js/logger/logger-basics/SKILL.md`; filtering — `@warlock.js/logger/filter-log-entries/SKILL.md`; competing `console.assert`, `console.time`, `console.timeEnd`, `perf_hooks.performance.now`.'
1068
+ ---
1069
+
1070
+ # Helpers — `assert`, `timer`
1071
+
1072
+ Two small DX shortcuts on every `Logger` (and the bound `log` helper). They route through the normal log pipeline — every channel sees what they emit.
1073
+
1074
+ ## `log.assert(condition, module, action, message, context?)`
1075
+
1076
+ Logs an `error` entry when `condition` is falsy. Genuinely free in the happy path: when the condition is truthy, the entry is never built and channels are never invoked.
1077
+
1078
+ ```ts
1079
+ log.assert(user !== null, "auth", "session", "user vanished mid-flight", {
1080
+ sessionId,
1081
+ });
1082
+
1083
+ // truthy → no log call
1084
+ // falsy → equivalent to log.error("auth", "session", "user vanished...", { sessionId })
1085
+ ```
1086
+
1087
+ The level is implicitly `error` — assertions express failures, not warnings. If you need a non-error level, use `log.error` / `log.warn` directly with your own `if`.
1088
+
1089
+ ### Why not `console.assert`?
1090
+
1091
+ `console.assert` writes to stderr only and bypasses your file/JSON channels. `log.assert` runs through the logger pipeline, so a failed assertion is captured by every persistent channel you've configured. See [`@warlock.js/logger/pick-log-channel/SKILL.md`](@warlock.js/logger/pick-log-channel/SKILL.md).
1092
+
1093
+ ## `log.timer(module, action)`
1094
+
1095
+ Returns an end-function. Calling it emits an `info` entry with `completed in <ms>ms` and a `durationMs` field in `context`.
1096
+
1097
+ ```ts
1098
+ const end = log.timer("db", "users.findById");
1099
+ const user = await usersRepo.findById(id);
1100
+ end({ id, found: !!user });
1101
+ // ℹ [db] [users.findById] completed in 12ms
1102
+ // ↳ { durationMs: 12, id: "abc", found: true } (when ConsoleLog has showContext: true)
1103
+ ```
1104
+
1105
+ Common patterns:
1106
+
1107
+ ```ts
1108
+ // Around an HTTP handler
1109
+ async function handle(req) {
1110
+ const end = log.timer("http", `${req.method} ${req.url}`);
1111
+ try {
1112
+ return await runHandler(req);
1113
+ } finally {
1114
+ end({ status: res.statusCode });
1115
+ }
1116
+ }
1117
+
1118
+ // Around a job
1119
+ const end = log.timer("jobs", "nightly-report");
1120
+ await report.run();
1121
+ end({ rowsProcessed: report.rowCount });
1122
+ ```
1123
+
1124
+ `end()` can be called more than once if you want intermediate checkpoints — each call emits a fresh entry with the duration measured from the original `timer()` call.
1125
+
1126
+ ### Caveats
1127
+
1128
+ - The duration is `Date.now()` based — millisecond resolution. For sub-millisecond profiling, reach for `performance.now()` directly.
1129
+ - The end-function captures `this` at construction; calling it after the logger is reconfigured still routes through the same `Logger` instance.
1130
+ - `log.timer` shorthand binds to the singleton — see [`@warlock.js/logger/test-logging-code/SKILL.md`](@warlock.js/logger/test-logging-code/SKILL.md) for how to swap channels per test.
1131
+
1132
+
1133
+ ## write-custom-log-channel `@warlock.js/logger/write-custom-log-channel/SKILL.md`
1134
+
1135
+ ---
1136
+ name: write-custom-log-channel
1137
+ description: 'Extend the abstract LogChannel class for custom sinks — Slack, database, HTTP endpoint, in-memory buffer. Triggers: `LogChannel`, `LogContract`, `LoggingData`, `shouldBeLogged`, `init`, `flushSync`, `terminal`; "log to slack", "log to a database", "send logs to datadog / loki HTTP api", "in-memory test capture channel", "build a custom log sink"; typical import `import { LogChannel, type LoggingData, type LogContract } from "@warlock.js/logger"`. Skip: built-in channels — `@warlock.js/logger/pick-log-channel/SKILL.md`; filtering — `@warlock.js/logger/filter-log-entries/SKILL.md`; competing libs `winston-transport`, `pino-transport`.'
1138
+ ---
1139
+
1140
+ # Custom channels — extending `LogChannel`
1141
+
1142
+ Build a sink for any destination — Slack, a database, an HTTP endpoint — by extending the abstract `LogChannel` class.
1143
+
1144
+ ## The 5-line minimum
1145
+
1146
+ ```ts
1147
+ import { LogChannel, type LoggingData } from "@warlock.js/logger";
1148
+
1149
+ export class NullChannel extends LogChannel {
1150
+ public name = "null";
1151
+ public log(_data: LoggingData) {}
1152
+ }
1153
+ ```
1154
+
1155
+ Then:
1156
+ ```ts
1157
+ log.addChannel(new NullChannel());
1158
+ ```
1159
+
1160
+ That's a working channel. `LogChannel` provides the scaffolding; you only need to supply `name` and `log()`.
1161
+
1162
+ ## What `LogChannel` gives you
1163
+
1164
+ | Thing | Who provides it |
1165
+ |---|---|
1166
+ | `name`, `description`, `terminal` | You (fields on your subclass) |
1167
+ | `log(data)` | **You must implement** — abstract |
1168
+ | `flushSync()` | You (optional — only if you buffer) |
1169
+ | `init()` | You (optional async hook — see below) |
1170
+ | `shouldBeLogged(data)` | `LogChannel` — combines `levels` + `filter` |
1171
+ | `config<K>(key)` | `LogChannel` — merges user config with `defaultConfigurations` |
1172
+ | `getDateAndTimeFormat()` | `LogChannel` — returns resolved `dateFormat` |
1173
+
1174
+ ## Complete example — SlackLog
1175
+
1176
+ ```ts title="src/channels/slack-log.ts"
1177
+ import { LogChannel, type BasicLogConfigurations, type LoggingData } from "@warlock.js/logger";
1178
+
1179
+ // `LogChannel<Options>` constrains `Options extends BasicLogConfigurations`,
1180
+ // so extend the base to keep the inherited levels / filter / redact options.
1181
+ type SlackConfig = BasicLogConfigurations & {
1182
+ webhookUrl: string;
1183
+ };
1184
+
1185
+ export class SlackLog extends LogChannel<SlackConfig> {
1186
+ public name = "slack";
1187
+ public description = "Posts errors + warnings to a Slack webhook";
1188
+
1189
+ public async log(data: LoggingData) {
1190
+ if (!this.shouldBeLogged(data)) return; // ← inherit levels + filter
1191
+
1192
+ await fetch(this.config("webhookUrl"), {
1193
+ method: "POST",
1194
+ headers: { "Content-Type": "application/json" },
1195
+ body: JSON.stringify({
1196
+ text: `[${data.type.toUpperCase()}] [${data.module}][${data.action}]: ${data.message}`,
1197
+ }),
1198
+ });
1199
+ }
1200
+ }
1201
+ ```
1202
+
1203
+ Register it alongside built-ins:
1204
+
1205
+ ```ts
1206
+ log.setChannels([
1207
+ new ConsoleLog(),
1208
+ new FileLog({ chunk: "daily" }),
1209
+ new SlackLog({
1210
+ webhookUrl: process.env.SLACK_WEBHOOK_URL!,
1211
+ levels: ["error", "warn"],
1212
+ }),
1213
+ ]);
1214
+ ```
1215
+
1216
+ ## The `init()` hook
1217
+
1218
+ Override `protected async init()` for one-time setup — open a socket, connect to a DB, prepare a write stream. Runs automatically after construction (inside a `setTimeout(0)`); `isInitialized` flips to `true` once resolved.
1219
+
1220
+ ```ts
1221
+ export class DatabaseLog extends LogChannel<
1222
+ BasicLogConfigurations & { connectionString: string }
1223
+ > {
1224
+ public name = "database";
1225
+ private client!: SomeDbClient;
1226
+
1227
+ protected async init() {
1228
+ this.client = await SomeDbClient.connect(this.config("connectionString"));
1229
+ }
1230
+
1231
+ public async log(data: LoggingData) {
1232
+ if (!this.shouldBeLogged(data)) return;
1233
+ await this.client.insert("logs", data);
1234
+ }
1235
+ }
1236
+ ```
1237
+
1238
+ ## Implementing `flushSync()`
1239
+
1240
+ Only if your channel buffers. Signature: `flushSync?(): void`. Synchronous — no `await`, no promises.
1241
+
1242
+ ```ts
1243
+ export class BatchHttpLog extends LogChannel<BasicLogConfigurations & { url: string }> {
1244
+ public name = "batch-http";
1245
+ private buffer: LoggingData[] = [];
1246
+
1247
+ public log(data: LoggingData) {
1248
+ if (!this.shouldBeLogged(data)) return;
1249
+ this.buffer.push(data);
1250
+ if (this.buffer.length >= 100) void this.drain();
1251
+ }
1252
+
1253
+ public flushSync() {
1254
+ // Synchronous HTTP — use `node:http` or `XMLHttpRequest` polyfill.
1255
+ // If sync HTTP isn't possible, at least dump the buffer to disk here
1256
+ // so a follow-up async drain can recover it next boot.
1257
+ }
1258
+
1259
+ private async drain() { /* async post to this.config("url") */ }
1260
+ }
1261
+ ```
1262
+
1263
+ ## The `terminal` property
1264
+
1265
+ - `terminal = true` (ConsoleLog default) → the logger passes the **original** message, ANSI codes intact.
1266
+ - `terminal = false` (base default, all file channels) → the logger passes a shallow-cloned copy whose `message` has ANSI codes stripped.
1267
+
1268
+ Set `terminal = true` on a channel only if its output is a TTY that should render colors.
1269
+
1270
+ ## `LogContract` — the minimal interface
1271
+
1272
+ If you don't want anything `LogChannel` provides (level filtering, config merging), implement `LogContract` directly:
1273
+
1274
+ ```ts
1275
+ import type { LogContract, LoggingData } from "@warlock.js/logger";
1276
+
1277
+ class MinimalSlack implements LogContract {
1278
+ public name = "slack";
1279
+
1280
+ public async log(data: LoggingData) {
1281
+ if (data.type !== "error") return;
1282
+ await fetch(process.env.SLACK_WEBHOOK!, { /* ... */ });
1283
+ }
1284
+ }
1285
+ ```
1286
+
1287
+ Prefer extending `LogChannel` unless you have a concrete reason not to — the level/filter plumbing is worth keeping.
1288
+
1289
+ ## Don't do
1290
+
1291
+ - Don't mutate `data` inside `log()`. Later channels see the mutation if the logger passes the same reference.
1292
+ - Don't throw synchronously from `log()`. The logger fires it without awaiting; an unhandled rejection takes down the process (unless `captureAnyUnhandledRejection` is wired up — and then it's embarrassing to be the cause).
1293
+ - Don't block the event loop. `log()` may be sync or async; if your work takes >100ms, make it async and return the promise.
1294
+ - Don't forget `shouldBeLogged(data)` at the top of `log()` — or your channel silently ignores `levels` / `filter` config.
1295
+
1296
+