@c9up/spectrum 0.1.12 → 0.1.14

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 (59) hide show
  1. package/README.md +30 -7
  2. package/dist/Logger.d.ts +14 -4
  3. package/dist/Logger.d.ts.map +1 -1
  4. package/dist/Logger.js +83 -49
  5. package/dist/Logger.js.map +1 -1
  6. package/dist/LoggerManager.d.ts.map +1 -1
  7. package/dist/LoggerManager.js +7 -1
  8. package/dist/LoggerManager.js.map +1 -1
  9. package/dist/RustLogBridge.d.ts.map +1 -1
  10. package/dist/RustLogBridge.js +76 -16
  11. package/dist/RustLogBridge.js.map +1 -1
  12. package/dist/SpectrumProvider.d.ts +9 -1
  13. package/dist/SpectrumProvider.d.ts.map +1 -1
  14. package/dist/SpectrumProvider.js +53 -28
  15. package/dist/SpectrumProvider.js.map +1 -1
  16. package/dist/augmentations.d.ts +29 -0
  17. package/dist/augmentations.d.ts.map +1 -0
  18. package/dist/augmentations.js +18 -0
  19. package/dist/augmentations.js.map +1 -0
  20. package/dist/channels/FileChannel.d.ts +1 -1
  21. package/dist/channels/FileChannel.d.ts.map +1 -1
  22. package/dist/channels/FileChannel.js +39 -9
  23. package/dist/channels/FileChannel.js.map +1 -1
  24. package/dist/configure.d.ts.map +1 -1
  25. package/dist/configure.js +19 -7
  26. package/dist/configure.js.map +1 -1
  27. package/dist/index.d.ts +2 -1
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +2 -1
  30. package/dist/index.js.map +1 -1
  31. package/dist/redact.d.ts +29 -0
  32. package/dist/redact.d.ts.map +1 -0
  33. package/dist/redact.js +90 -0
  34. package/dist/redact.js.map +1 -0
  35. package/dist/services/main.d.ts +9 -0
  36. package/dist/services/main.d.ts.map +1 -1
  37. package/dist/services/main.js +11 -0
  38. package/dist/services/main.js.map +1 -1
  39. package/dist/targets.d.ts +6 -1
  40. package/dist/targets.d.ts.map +1 -1
  41. package/dist/targets.js +59 -5
  42. package/dist/targets.js.map +1 -1
  43. package/dist/types.d.ts +38 -1
  44. package/dist/types.d.ts.map +1 -1
  45. package/dist/types.js +33 -6
  46. package/dist/types.js.map +1 -1
  47. package/package.json +4 -2
  48. package/src/Logger.ts +113 -51
  49. package/src/LoggerManager.ts +9 -1
  50. package/src/RustLogBridge.ts +95 -20
  51. package/src/SpectrumProvider.ts +62 -32
  52. package/src/augmentations.ts +32 -0
  53. package/src/channels/FileChannel.ts +43 -9
  54. package/src/configure.ts +19 -7
  55. package/src/index.ts +4 -0
  56. package/src/redact.ts +100 -0
  57. package/src/services/main.ts +12 -0
  58. package/src/targets.ts +78 -12
  59. package/src/types.ts +60 -7
package/dist/targets.js CHANGED
@@ -15,6 +15,7 @@
15
15
  */
16
16
  import { ConsoleChannel } from "./channels/ConsoleChannel.js";
17
17
  import { FileChannel } from "./channels/FileChannel.js";
18
+ import { isLogLevelWithSilent, LOG_LEVEL_ORDER, levelThreshold, } from "./types.js";
18
19
  /** Builds the targets array conditionally. */
19
20
  export class Targets {
20
21
  #targets = [];
@@ -49,6 +50,53 @@ export const targets = Object.assign(() => new Targets(), {
49
50
  file: fileTarget,
50
51
  pretty: prettyTarget,
51
52
  });
53
+ /**
54
+ * A channel that only sees the entries its target's own level admits.
55
+ *
56
+ * `targets.file({ destination: 'app.log' }, 'error')` is a statement about that
57
+ * destination, not about the logger: upstream gives every transport its own
58
+ * level and pino filters per target. Dropping it sent every debug line — request
59
+ * bodies, tokens, whatever the app logs while it is being debugged — into the
60
+ * file the deployment had scoped down to errors.
61
+ */
62
+ class LeveledChannel {
63
+ name;
64
+ #inner;
65
+ #threshold;
66
+ constructor(inner, threshold) {
67
+ this.#inner = inner;
68
+ this.#threshold = threshold;
69
+ this.name = inner.name;
70
+ }
71
+ write(entry) {
72
+ if (LOG_LEVEL_ORDER[entry.level] < this.#threshold)
73
+ return;
74
+ this.#inner.write(entry);
75
+ }
76
+ /**
77
+ * Forwarded because the wrapper is now what the provider holds, and a
78
+ * FileChannel that is never closed keeps its stream open past shutdown.
79
+ */
80
+ close() {
81
+ const inner = this.#inner;
82
+ inner.close?.();
83
+ }
84
+ }
85
+ /** Gate a channel on a target level, when the target names one. */
86
+ function atLevel(channel, level) {
87
+ if (level === undefined)
88
+ return channel;
89
+ if (!isLogLevelWithSilent(level)) {
90
+ // A level nobody can act on is a config error, and the failure mode of
91
+ // ignoring it is the destination receiving everything.
92
+ throw new Error(`[spectrum] Unknown transport target level "${level}". Use one of: ${Object.keys(LOG_LEVEL_ORDER).join(", ")}, silent.`);
93
+ }
94
+ const threshold = levelThreshold(level);
95
+ // `trace` admits everything the logger already let through.
96
+ if (threshold <= LOG_LEVEL_ORDER.trace)
97
+ return channel;
98
+ return new LeveledChannel(channel, threshold);
99
+ }
52
100
  /**
53
101
  * Turn transport targets into the channels spectrum actually writes through.
54
102
  *
@@ -56,17 +104,23 @@ export const targets = Object.assign(() => new Targets(), {
56
104
  * file when `destination` is a path, and to the console when it is a file
57
105
  * descriptor, which is what `destination: 1` means in a containerised app that
58
106
  * ships stdout to its log collector.
107
+ *
108
+ * A target with no level of its own inherits `inheritedLevel`, the level of the
109
+ * logger it belongs to — upstream's `defineConfig` fills the same gap, by
110
+ * writing the level into the target. Passing it instead of writing it leaves
111
+ * the caller's config object alone.
59
112
  */
60
- export function channelsFromTargets(list) {
113
+ export function channelsFromTargets(list, inheritedLevel) {
61
114
  const channels = [];
62
115
  for (const entry of list) {
116
+ const level = entry.level ?? inheritedLevel;
63
117
  if (entry.target === "pino-pretty") {
64
- channels.push(new ConsoleChannel("pretty"));
118
+ channels.push(atLevel(new ConsoleChannel("pretty"), level));
65
119
  continue;
66
120
  }
67
121
  const destination = entry.options?.destination;
68
122
  if (typeof destination === "string") {
69
- channels.push(new FileChannel({
123
+ channels.push(atLevel(new FileChannel({
70
124
  path: destination,
71
125
  ...(typeof entry.options?.maxSizeBytes === "number"
72
126
  ? { maxSizeBytes: entry.options.maxSizeBytes }
@@ -74,12 +128,12 @@ export function channelsFromTargets(list) {
74
128
  ...(typeof entry.options?.maxFiles === "number"
75
129
  ? { maxFiles: entry.options.maxFiles }
76
130
  : {}),
77
- }));
131
+ }), level));
78
132
  continue;
79
133
  }
80
134
  // A descriptor (or nothing) means the process's own output, as JSON —
81
135
  // the shape a log collector parses.
82
- channels.push(new ConsoleChannel("json"));
136
+ channels.push(atLevel(new ConsoleChannel("json"), level));
83
137
  }
84
138
  return channels;
85
139
  }
@@ -1 +1 @@
1
- {"version":3,"file":"targets.js","sourceRoot":"","sources":["../src/targets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAuBxD,8CAA8C;AAC9C,MAAM,OAAO,OAAO;IACV,QAAQ,GAA6B,EAAE,CAAC;IAEjD,IAAI,CAAC,KAA6B;QACjC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,oDAAoD;IACpD,MAAM,CACL,WAAoB,EACpB,KAA8D;QAE9D,0EAA0E;QAC1E,yCAAyC;QACzC,IAAI,WAAW;YAAE,IAAI,CAAC,IAAI,CAAC,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC1E,OAAO,IAAI,CAAC;IACb,CAAC;IAED,iDAAiD;IACjD,UAAU,CACT,WAAoB,EACpB,KAA8D;QAE9D,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IACzC,CAAC;IAED,OAAO;QACN,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC3B,CAAC;CACD;AAED,SAAS,UAAU,CAClB,UAA6B,EAAE,EAC/B,KAAmC;IAEnC,OAAO,EAAE,MAAM,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BAClD,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,YAAY,EAAE;oBAC9C,CAAC,CAAC,EAAE,CAAC;gBACN,GAAG,CAAC,OAAO,KAAK,CAAC,OAAO,EAAE,QAAQ,KAAK,QAAQ;oBAC9C,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE;oBACtC,CAAC,CAAC,EAAE,CAAC;aACN,CAAC,EACF,KAAK,CACL,CACD,CAAC;YACF,SAAS;QACV,CAAC;QACD,sEAAsE;QACtE,oCAAoC;QACpC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC"}
package/dist/types.d.ts CHANGED
@@ -6,11 +6,30 @@ import type { TransportTargetOptions } from "./targets.js";
6
6
  export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
7
7
  /** A configured level, including pino's `silent` (disables all output). */
8
8
  export type LogLevelWithSilent = LogLevel | "silent";
9
+ /**
10
+ * The numeric value of each level, on pino's scale.
11
+ *
12
+ * Only the ORDER matters to the comparisons here, but the numbers are what
13
+ * `logger.levels` and `logger.levelNumber` hand back, and those are read
14
+ * against the ecosystem's values: an `info` line is 30 everywhere upstream
15
+ * reaches, so it is 30 here.
16
+ */
9
17
  export declare const LOG_LEVEL_ORDER: Record<LogLevel, number>;
10
18
  /** Threshold for the `silent` level — nothing is ever above it. */
11
19
  export declare const SILENT_THRESHOLD: number;
12
20
  /** Numeric threshold for a configured level (`silent` → +Infinity). */
13
21
  export declare function levelThreshold(level: LogLevelWithSilent): number;
22
+ /** Whether a value is a level a config may name. */
23
+ export declare function isLogLevelWithSilent(value: unknown): value is LogLevelWithSilent;
24
+ /**
25
+ * Narrow an untyped level — `process.env.LOG_LEVEL`, a CLI flag — to one the
26
+ * config can name, falling back to `info`.
27
+ *
28
+ * Without it a config file has to assert the env string into the union, and an
29
+ * assertion is the wrong tool for a value that comes from outside the program:
30
+ * it claims to know what it cannot. This checks.
31
+ */
32
+ export declare function logLevel(raw: unknown, fallback?: LogLevelWithSilent): LogLevelWithSilent;
14
33
  export interface LogEntry {
15
34
  level: LogLevel;
16
35
  message: string;
@@ -33,6 +52,16 @@ export type { TransportTargetOptions } from "./targets.js";
33
52
  export interface LogChannel {
34
53
  name: string;
35
54
  write(entry: LogEntry): void;
55
+ /**
56
+ * Release whatever the channel holds open, and finish writing.
57
+ *
58
+ * Allowed to be asynchronous, and that is the point: a file channel ends a
59
+ * stream, and ending one is a request, not a completion. Returning before
60
+ * the `finish` event meant a shutdown could reach `process.exit` with the
61
+ * last lines still in the kernel's hands — the lines about why the process
62
+ * was shutting down, most of the time.
63
+ */
64
+ close?(): void | Promise<void>;
36
65
  }
37
66
  /** Transforms a value before it is written (pino `serializers` parity). */
38
67
  export type LogSerializer = (value: unknown) => unknown;
@@ -46,7 +75,15 @@ export interface RedactOptions {
46
75
  * / pino, with spectrum divergences called out inline.
47
76
  */
48
77
  export interface LogConfig {
49
- /** Turn the logger off while keeping its API callable (no-op). Default: true. */
78
+ /**
79
+ * Turn the logger off while keeping its API callable (no-op). Default: true.
80
+ *
81
+ * Named deviation: upstream reads this as `!!config.enabled`, so a config
82
+ * that never writes it has a logger that silently logs nothing. That only
83
+ * works because upstream's own generated config always writes `enabled:
84
+ * true` — a default that turns the feature off is a trap for every config
85
+ * written by hand.
86
+ */
50
87
  enabled?: boolean;
51
88
  /** Optional logger name (assigned by the manager when omitted). */
52
89
  name?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAC3D;;;GAGG;AAEH,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAA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,sBAAsB,EAAE,CAAA;KAAE,CAAC;IAC5D,wDAAwD;IACxD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC7C,uEAAuE;IACvE,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,aAAa,CAAC;IAClC,oEAAoE;IACpE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CAC5C;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;CACnC;AAWD,mFAAmF;AACnF,wBAAgB,cAAc,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAMvE"}
package/dist/types.js CHANGED
@@ -1,10 +1,18 @@
1
+ /**
2
+ * The numeric value of each level, on pino's scale.
3
+ *
4
+ * Only the ORDER matters to the comparisons here, but the numbers are what
5
+ * `logger.levels` and `logger.levelNumber` hand back, and those are read
6
+ * against the ecosystem's values: an `info` line is 30 everywhere upstream
7
+ * reaches, so it is 30 here.
8
+ */
1
9
  export const LOG_LEVEL_ORDER = {
2
- trace: 0,
3
- debug: 1,
4
- info: 2,
5
- warn: 3,
6
- error: 4,
7
- fatal: 5,
10
+ trace: 10,
11
+ debug: 20,
12
+ info: 30,
13
+ warn: 40,
14
+ error: 50,
15
+ fatal: 60,
8
16
  };
9
17
  /** Threshold for the `silent` level — nothing is ever above it. */
10
18
  export const SILENT_THRESHOLD = Number.POSITIVE_INFINITY;
@@ -12,6 +20,25 @@ export const SILENT_THRESHOLD = Number.POSITIVE_INFINITY;
12
20
  export function levelThreshold(level) {
13
21
  return level === "silent" ? SILENT_THRESHOLD : LOG_LEVEL_ORDER[level];
14
22
  }
23
+ const CONFIGURED_LEVELS = new Set([
24
+ ...Object.keys(LOG_LEVEL_ORDER),
25
+ "silent",
26
+ ]);
27
+ /** Whether a value is a level a config may name. */
28
+ export function isLogLevelWithSilent(value) {
29
+ return typeof value === "string" && CONFIGURED_LEVELS.has(value);
30
+ }
31
+ /**
32
+ * Narrow an untyped level — `process.env.LOG_LEVEL`, a CLI flag — to one the
33
+ * config can name, falling back to `info`.
34
+ *
35
+ * Without it a config file has to assert the env string into the union, and an
36
+ * assertion is the wrong tool for a value that comes from outside the program:
37
+ * it claims to know what it cannot. This checks.
38
+ */
39
+ export function logLevel(raw, fallback = "info") {
40
+ return isLogLevelWithSilent(raw) ? raw : fallback;
41
+ }
15
42
  const RESERVED_ENTRY_KEYS = new Set([
16
43
  "level",
17
44
  "message",
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAWA,MAAM,CAAC,MAAM,eAAe,GAA6B;IACxD,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;CACR,CAAC;AAEF,mEAAmE;AACnE,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,iBAAiB,CAAC;AAEzD,uEAAuE;AACvE,MAAM,UAAU,cAAc,CAAC,KAAyB;IACvD,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;AACvE,CAAC;AAyED,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAS;IAC3C,OAAO;IACP,SAAS;IACT,QAAQ;IACR,eAAe;IACf,WAAW;IACX,MAAM;CACN,CAAC,CAAC;AAEH,mFAAmF;AACnF,MAAM,UAAU,cAAc,CAAC,KAAe;IAC7C,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAA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;IACR,eAAe;IACf,WAAW;IACX,MAAM;CACN,CAAC,CAAC;AAEH,mFAAmF;AACnF,MAAM,UAAU,cAAc,CAAC,KAAe;IAC7C,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACtC,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c9up/spectrum",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "Spectrum — structured logging for the Ream framework",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,7 +33,9 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@biomejs/biome": "^2.4.10",
36
+ "@c9up/ream": "^0.2.0",
36
37
  "@types/node": "^22.19.15",
38
+ "@vitest/coverage-v8": "^4.1.2",
37
39
  "typescript": "^6.0.2",
38
40
  "vitest": "^4.1.2"
39
41
  },
@@ -62,7 +64,7 @@
62
64
  "prebuild": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
63
65
  "build": "tsc -p tsconfig.build.json",
64
66
  "test": "vitest run",
65
- "lint": "biome check src/",
67
+ "lint": "biome check src/ tests/",
66
68
  "test:coverage": "vitest run --coverage",
67
69
  "typecheck": "tsc --noEmit"
68
70
  }
package/src/Logger.ts CHANGED
@@ -14,6 +14,7 @@
14
14
  */
15
15
 
16
16
  import { format } from "node:util";
17
+ import { parseRedactPath, redactPath } from "./redact.js";
17
18
  import { sanitizeLogValue } from "./sanitize.js";
18
19
  import { channelsFromTargets } from "./targets.js";
19
20
  import type {
@@ -44,15 +45,51 @@ function isMergeableObject(value: unknown): value is Record<string, unknown> {
44
45
  );
45
46
  }
46
47
 
48
+ /** How far down a `cause` chain to follow before calling it a cycle. */
49
+ const MAX_CAUSE_DEPTH = 8;
50
+
47
51
  /**
48
- * Serialize an Error into a plain `{ name, message, stack }` — the pino `err`
49
- * serializer parity. Non-Error values pass through untouched.
52
+ * Serialize an Error the way pino's `err` serializer does. Non-Error values
53
+ * pass through untouched.
54
+ *
55
+ * `{ name, message, stack }` threw away everything the error was carrying.
56
+ * `err.code` is usually the one field a log search is keyed on, and the `cause`
57
+ * of a wrapped error is the half that says what actually failed — both were
58
+ * dropped, silently, by a serializer whose job is to keep them.
50
59
  */
51
- function serializeError(value: unknown): unknown {
52
- if (value instanceof Error) {
53
- return { name: value.name, message: value.message, stack: value.stack };
60
+ function serializeError(value: unknown, depth = 0): unknown {
61
+ if (!(value instanceof Error)) return value;
62
+
63
+ // Own ENUMERABLE properties only, which on an Error means the ones it was
64
+ // given: `message`, `stack` and `name` are not among them.
65
+ const out: Record<string, unknown> = { ...value };
66
+ for (const [key, nested] of Object.entries(out)) {
67
+ if (nested instanceof Error && depth < MAX_CAUSE_DEPTH) {
68
+ out[key] = serializeError(nested, depth + 1);
69
+ }
70
+ }
71
+
72
+ // Upstream reads the constructor rather than `name`, so a subclass that
73
+ // never assigns `this.name` is still reported as itself, and it names the
74
+ // field `type` — which is what an Adonis log pipeline reads.
75
+ out.type =
76
+ typeof value.constructor === "function"
77
+ ? value.constructor.name
78
+ : value.name;
79
+ out.message = value.message;
80
+ out.stack = value.stack;
81
+ if (value instanceof AggregateError && Array.isArray(value.errors)) {
82
+ out.aggregateErrors = value.errors.map((each) =>
83
+ depth < MAX_CAUSE_DEPTH ? serializeError(each, depth + 1) : String(each),
84
+ );
85
+ }
86
+ if (value.cause !== undefined) {
87
+ out.cause =
88
+ depth < MAX_CAUSE_DEPTH
89
+ ? serializeError(value.cause, depth + 1)
90
+ : "[cause chain too deep]";
54
91
  }
55
- return value;
92
+ return out;
56
93
  }
57
94
 
58
95
  const DEFAULT_SERIALIZERS: Record<string, LogSerializer> = {
@@ -60,36 +97,22 @@ const DEFAULT_SERIALIZERS: Record<string, LogSerializer> = {
60
97
  error: serializeError,
61
98
  };
62
99
 
63
- /**
64
- * Copy-on-write redaction of a single dot-path. Never mutates the input; only
65
- * the objects along the path are shallow-copied.
66
- */
67
- function redactPath(
68
- obj: Record<string, unknown>,
69
- path: string,
70
- censor: string,
71
- ): Record<string, unknown> {
72
- const dot = path.indexOf(".");
73
- const head = dot === -1 ? path : path.slice(0, dot);
74
- if (!(head in obj)) return obj;
75
-
76
- const copy: Record<string, unknown> = { ...obj };
77
- if (dot === -1) {
78
- copy[head] = censor;
79
- } else {
80
- const child = copy[head];
81
- if (isMergeableObject(child)) {
82
- copy[head] = redactPath(child, path.slice(dot + 1), censor);
83
- }
84
- }
85
- return copy;
86
- }
87
-
88
100
  export class Logger {
89
101
  #config: LogConfig;
90
102
  #module: string;
91
103
  #correlationId?: string;
92
104
  #bindings: Record<string, unknown>;
105
+ /**
106
+ * This instance's level, when it has been set on the instance.
107
+ *
108
+ * The config object is shared by reference with the parent, every sibling
109
+ * child and the manager, so writing the level into it made
110
+ * `child.level = 'debug'` turn on debug for the whole application. pino
111
+ * keeps the level on the logger; so does this.
112
+ */
113
+ #level?: LogLevelWithSilent;
114
+ /** `config.redact` compiled once, as pino compiles its redactor once. */
115
+ readonly #redactPaths: string[][];
93
116
 
94
117
  constructor(
95
118
  config: LogConfig,
@@ -103,11 +126,21 @@ export class Logger {
103
126
  // specific statement of intent.
104
127
  this.#config =
105
128
  config.channels === undefined && config.transport?.targets !== undefined
106
- ? { ...config, channels: channelsFromTargets(config.transport.targets) }
129
+ ? {
130
+ ...config,
131
+ channels: channelsFromTargets(
132
+ config.transport.targets,
133
+ config.level,
134
+ ),
135
+ }
107
136
  : config;
108
137
  this.#module = module;
109
138
  this.#correlationId = correlationId;
110
139
  this.#bindings = bindings;
140
+ const redact = this.#config.redact;
141
+ const paths =
142
+ redact === undefined ? [] : Array.isArray(redact) ? redact : redact.paths;
143
+ this.#redactPaths = paths.map(parseRedactPath);
111
144
  }
112
145
 
113
146
  /** Whether the logger is enabled. When false every log call is a no-op. */
@@ -115,13 +148,13 @@ export class Logger {
115
148
  return this.#config.enabled !== false;
116
149
  }
117
150
 
118
- /** The configured level for this logger. */
151
+ /** The level this logger is at. */
119
152
  get level(): LogLevelWithSilent {
120
- return this.#config.level ?? "info";
153
+ return this.#level ?? this.#config.level ?? "info";
121
154
  }
122
155
 
123
156
  set level(level: LogLevelWithSilent) {
124
- this.#config.level = level;
157
+ this.#level = level;
125
158
  }
126
159
 
127
160
  /** Numeric value of the current level (`silent` → +Infinity). */
@@ -150,9 +183,22 @@ export class Logger {
150
183
  /**
151
184
  * Run `callback` only when `level` is enabled — useful to guard expensive
152
185
  * log-data computation.
186
+ *
187
+ * An async callback's promise is HANDED BACK, as upstream hands it back.
188
+ * Swallowing it turned a rejection inside the guarded block into an
189
+ * unhandled rejection with no caller able to await it.
153
190
  */
154
- ifLevelEnabled(level: LogLevel, callback: (logger: this) => void): void {
155
- if (this.isLevelEnabled(level)) callback(this);
191
+ ifLevelEnabled(
192
+ level: LogLevel,
193
+ callback: (logger: this) => Promise<void>,
194
+ ): Promise<void> | undefined;
195
+ ifLevelEnabled(level: LogLevel, callback: (logger: this) => void): void;
196
+ ifLevelEnabled(
197
+ level: LogLevel,
198
+ callback: (logger: this) => unknown,
199
+ ): unknown {
200
+ if (this.isLevelEnabled(level)) return callback(this);
201
+ return undefined;
156
202
  }
157
203
 
158
204
  /**
@@ -166,15 +212,21 @@ export class Logger {
166
212
  const nextModule = typeof module === "string" ? module : this.#module;
167
213
  const nextCorrelation =
168
214
  typeof correlationId === "string" ? correlationId : this.#correlationId;
169
- return new Logger(this.#config, nextModule, nextCorrelation, {
215
+ const child = new Logger(this.#config, nextModule, nextCorrelation, {
170
216
  ...this.#bindings,
171
217
  ...rest,
172
218
  });
219
+ // A child starts where its parent stands now, and moves on its own
220
+ // afterwards — pino's semantics, and the reason the level cannot live
221
+ // in the config the two of them share.
222
+ if (this.#level !== undefined) child.level = this.#level;
223
+ return child;
173
224
  }
174
225
 
175
- /** Current bindings (child bindings + module/correlationId conventions). */
226
+ /** Current bindings (logger name + child bindings + module/correlationId). */
176
227
  bindings(): Record<string, unknown> {
177
228
  const out: Record<string, unknown> = {
229
+ ...(this.#config.name !== undefined ? { name: this.#config.name } : {}),
178
230
  ...this.#bindings,
179
231
  module: this.#module,
180
232
  };
@@ -238,12 +290,18 @@ export class Logger {
238
290
  this.#write("fatal", arg0, rest);
239
291
  }
240
292
 
241
- /** No-op — pino API-surface parity for the `silent` level. */
293
+ /**
294
+ * No-op — the `silent` level exists so a call site can be written and stay
295
+ * written. It takes the same arguments as every other level, because
296
+ * upstream's does and switching a call to it must not stop compiling.
297
+ */
298
+ silent(mergingObject: unknown, message?: string, ...values: unknown[]): void;
299
+ silent(message: string, ...values: unknown[]): void;
242
300
  silent(): void {}
243
301
 
244
302
  /** Resolve the numeric threshold for this logger + module. */
245
303
  #threshold(): number {
246
- const raw = this.#config.modules?.[this.#module] ?? this.#config.level;
304
+ const raw = this.#config.modules?.[this.#module] ?? this.level;
247
305
  if (raw === "silent") return levelThreshold("silent");
248
306
  if (typeof raw === "string" && isLogLevel(raw)) return LOG_LEVEL_ORDER[raw];
249
307
  return LOG_LEVEL_ORDER.info;
@@ -302,18 +360,17 @@ export class Logger {
302
360
  }
303
361
 
304
362
  #applyRedact(bag: Record<string, unknown>): Record<string, unknown> {
363
+ if (this.#redactPaths.length === 0) return bag;
305
364
  const redact = this.#config.redact;
306
- if (!redact) return bag;
307
- const paths = Array.isArray(redact) ? redact : redact.paths;
308
- if (paths.length === 0) return bag;
309
- const censor = Array.isArray(redact)
310
- ? "[Redacted]"
311
- : (redact.censor ?? "[Redacted]");
312
- let out = bag;
313
- for (const path of paths) {
314
- out = redactPath(out, path, censor);
365
+ const censor =
366
+ redact === undefined || Array.isArray(redact)
367
+ ? "[Redacted]"
368
+ : (redact.censor ?? "[Redacted]");
369
+ let out: unknown = bag;
370
+ for (const tokens of this.#redactPaths) {
371
+ out = redactPath(out, tokens, censor);
315
372
  }
316
- return out;
373
+ return isMergeableObject(out) ? out : bag;
317
374
  }
318
375
 
319
376
  #write(level: LogLevel, arg0: unknown, rest: unknown[]): void {
@@ -323,6 +380,11 @@ export class Logger {
323
380
  const { message, mergingObject } = this.#resolveArgs(arg0, rest);
324
381
 
325
382
  let bag: Record<string, unknown> = {
383
+ // The name a multi-logger config gave this logger. It was assigned by
384
+ // the manager and then read by nothing, so two loggers writing to the
385
+ // same destination produced lines with no way to tell them apart.
386
+ // pino carries it as a binding; a merging object still wins over it.
387
+ ...(this.#config.name !== undefined ? { name: this.#config.name } : {}),
326
388
  ...this.#bindings,
327
389
  ...(mergingObject ?? {}),
328
390
  };
@@ -13,7 +13,15 @@ export class LoggerManager extends Logger {
13
13
  #loggers: Map<string, Logger> = new Map();
14
14
 
15
15
  constructor(config: LoggerManagerConfig) {
16
- super(config.loggers[config.default]);
16
+ const selected = config.loggers[config.default];
17
+ if (selected === undefined) {
18
+ // Naming a logger that is not in `loggers` used to build a manager
19
+ // around undefined and fail on the first line written.
20
+ throw new Error(
21
+ `[spectrum] config.logger names '${config.default}', which is not in \`loggers\`. Declared: ${Object.keys(config.loggers).join(", ") || "(none)"}.`,
22
+ );
23
+ }
24
+ super(selected);
17
25
  this.#config = config;
18
26
  }
19
27