@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/src/targets.ts CHANGED
@@ -16,7 +16,12 @@
16
16
 
17
17
  import { ConsoleChannel } from "./channels/ConsoleChannel.js";
18
18
  import { FileChannel } from "./channels/FileChannel.js";
19
- import type { LogChannel, LogLevelWithSilent } from "./types.js";
19
+ import type { LogChannel, LogEntry, LogLevelWithSilent } from "./types.js";
20
+ import {
21
+ isLogLevelWithSilent,
22
+ LOG_LEVEL_ORDER,
23
+ levelThreshold,
24
+ } from "./types.js";
20
25
 
21
26
  /** One transport entry (AdonisJS `TransportTargetOptions`). */
22
27
  export interface TransportTargetOptions {
@@ -94,6 +99,57 @@ export const targets: (() => Targets) & {
94
99
  pretty: prettyTarget,
95
100
  });
96
101
 
102
+ /**
103
+ * A channel that only sees the entries its target's own level admits.
104
+ *
105
+ * `targets.file({ destination: 'app.log' }, 'error')` is a statement about that
106
+ * destination, not about the logger: upstream gives every transport its own
107
+ * level and pino filters per target. Dropping it sent every debug line — request
108
+ * bodies, tokens, whatever the app logs while it is being debugged — into the
109
+ * file the deployment had scoped down to errors.
110
+ */
111
+ class LeveledChannel implements LogChannel {
112
+ readonly name: string;
113
+ readonly #inner: LogChannel;
114
+ readonly #threshold: number;
115
+
116
+ constructor(inner: LogChannel, threshold: number) {
117
+ this.#inner = inner;
118
+ this.#threshold = threshold;
119
+ this.name = inner.name;
120
+ }
121
+
122
+ write(entry: LogEntry): void {
123
+ if (LOG_LEVEL_ORDER[entry.level] < this.#threshold) return;
124
+ this.#inner.write(entry);
125
+ }
126
+
127
+ /**
128
+ * Forwarded because the wrapper is now what the provider holds, and a
129
+ * FileChannel that is never closed keeps its stream open past shutdown.
130
+ */
131
+ close(): void {
132
+ const inner: LogChannel & { close?: () => void } = this.#inner;
133
+ inner.close?.();
134
+ }
135
+ }
136
+
137
+ /** Gate a channel on a target level, when the target names one. */
138
+ function atLevel(channel: LogChannel, level: string | undefined): LogChannel {
139
+ if (level === undefined) return channel;
140
+ if (!isLogLevelWithSilent(level)) {
141
+ // A level nobody can act on is a config error, and the failure mode of
142
+ // ignoring it is the destination receiving everything.
143
+ throw new Error(
144
+ `[spectrum] Unknown transport target level "${level}". Use one of: ${Object.keys(LOG_LEVEL_ORDER).join(", ")}, silent.`,
145
+ );
146
+ }
147
+ const threshold = levelThreshold(level);
148
+ // `trace` admits everything the logger already let through.
149
+ if (threshold <= LOG_LEVEL_ORDER.trace) return channel;
150
+ return new LeveledChannel(channel, threshold);
151
+ }
152
+
97
153
  /**
98
154
  * Turn transport targets into the channels spectrum actually writes through.
99
155
  *
@@ -101,34 +157,44 @@ export const targets: (() => Targets) & {
101
157
  * file when `destination` is a path, and to the console when it is a file
102
158
  * descriptor, which is what `destination: 1` means in a containerised app that
103
159
  * ships stdout to its log collector.
160
+ *
161
+ * A target with no level of its own inherits `inheritedLevel`, the level of the
162
+ * logger it belongs to — upstream's `defineConfig` fills the same gap, by
163
+ * writing the level into the target. Passing it instead of writing it leaves
164
+ * the caller's config object alone.
104
165
  */
105
166
  export function channelsFromTargets(
106
167
  list: readonly TransportTargetOptions[],
168
+ inheritedLevel?: LogLevelWithSilent | string,
107
169
  ): LogChannel[] {
108
170
  const channels: LogChannel[] = [];
109
171
  for (const entry of list) {
172
+ const level = entry.level ?? inheritedLevel;
110
173
  if (entry.target === "pino-pretty") {
111
- channels.push(new ConsoleChannel("pretty"));
174
+ channels.push(atLevel(new ConsoleChannel("pretty"), level));
112
175
  continue;
113
176
  }
114
177
  const destination = entry.options?.destination;
115
178
  if (typeof destination === "string") {
116
179
  channels.push(
117
- new FileChannel({
118
- path: destination,
119
- ...(typeof entry.options?.maxSizeBytes === "number"
120
- ? { maxSizeBytes: entry.options.maxSizeBytes }
121
- : {}),
122
- ...(typeof entry.options?.maxFiles === "number"
123
- ? { maxFiles: entry.options.maxFiles }
124
- : {}),
125
- }),
180
+ atLevel(
181
+ new FileChannel({
182
+ path: destination,
183
+ ...(typeof entry.options?.maxSizeBytes === "number"
184
+ ? { maxSizeBytes: entry.options.maxSizeBytes }
185
+ : {}),
186
+ ...(typeof entry.options?.maxFiles === "number"
187
+ ? { maxFiles: entry.options.maxFiles }
188
+ : {}),
189
+ }),
190
+ level,
191
+ ),
126
192
  );
127
193
  continue;
128
194
  }
129
195
  // A descriptor (or nothing) means the process's own output, as JSON —
130
196
  // the shape a log collector parses.
131
- channels.push(new ConsoleChannel("json"));
197
+ channels.push(atLevel(new ConsoleChannel("json"), level));
132
198
  }
133
199
  return channels;
134
200
  }
package/src/types.ts CHANGED
@@ -9,13 +9,21 @@ export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
9
9
  /** A configured level, including pino's `silent` (disables all output). */
10
10
  export type LogLevelWithSilent = LogLevel | "silent";
11
11
 
12
+ /**
13
+ * The numeric value of each level, on pino's scale.
14
+ *
15
+ * Only the ORDER matters to the comparisons here, but the numbers are what
16
+ * `logger.levels` and `logger.levelNumber` hand back, and those are read
17
+ * against the ecosystem's values: an `info` line is 30 everywhere upstream
18
+ * reaches, so it is 30 here.
19
+ */
12
20
  export const LOG_LEVEL_ORDER: Record<LogLevel, number> = {
13
- trace: 0,
14
- debug: 1,
15
- info: 2,
16
- warn: 3,
17
- error: 4,
18
- fatal: 5,
21
+ trace: 10,
22
+ debug: 20,
23
+ info: 30,
24
+ warn: 40,
25
+ error: 50,
26
+ fatal: 60,
19
27
  };
20
28
 
21
29
  /** Threshold for the `silent` level — nothing is ever above it. */
@@ -26,6 +34,33 @@ export function levelThreshold(level: LogLevelWithSilent): number {
26
34
  return level === "silent" ? SILENT_THRESHOLD : LOG_LEVEL_ORDER[level];
27
35
  }
28
36
 
37
+ const CONFIGURED_LEVELS = new Set<string>([
38
+ ...Object.keys(LOG_LEVEL_ORDER),
39
+ "silent",
40
+ ]);
41
+
42
+ /** Whether a value is a level a config may name. */
43
+ export function isLogLevelWithSilent(
44
+ value: unknown,
45
+ ): value is LogLevelWithSilent {
46
+ return typeof value === "string" && CONFIGURED_LEVELS.has(value);
47
+ }
48
+
49
+ /**
50
+ * Narrow an untyped level — `process.env.LOG_LEVEL`, a CLI flag — to one the
51
+ * config can name, falling back to `info`.
52
+ *
53
+ * Without it a config file has to assert the env string into the union, and an
54
+ * assertion is the wrong tool for a value that comes from outside the program:
55
+ * it claims to know what it cannot. This checks.
56
+ */
57
+ export function logLevel(
58
+ raw: unknown,
59
+ fallback: LogLevelWithSilent = "info",
60
+ ): LogLevelWithSilent {
61
+ return isLogLevelWithSilent(raw) ? raw : fallback;
62
+ }
63
+
29
64
  export interface LogEntry {
30
65
  level: LogLevel;
31
66
  message: string;
@@ -50,6 +85,16 @@ export type { TransportTargetOptions } from "./targets.js";
50
85
  export interface LogChannel {
51
86
  name: string;
52
87
  write(entry: LogEntry): void;
88
+ /**
89
+ * Release whatever the channel holds open, and finish writing.
90
+ *
91
+ * Allowed to be asynchronous, and that is the point: a file channel ends a
92
+ * stream, and ending one is a request, not a completion. Returning before
93
+ * the `finish` event meant a shutdown could reach `process.exit` with the
94
+ * last lines still in the kernel's hands — the lines about why the process
95
+ * was shutting down, most of the time.
96
+ */
97
+ close?(): void | Promise<void>;
53
98
  }
54
99
 
55
100
  /** Transforms a value before it is written (pino `serializers` parity). */
@@ -66,7 +111,15 @@ export interface RedactOptions {
66
111
  * / pino, with spectrum divergences called out inline.
67
112
  */
68
113
  export interface LogConfig {
69
- /** Turn the logger off while keeping its API callable (no-op). Default: true. */
114
+ /**
115
+ * Turn the logger off while keeping its API callable (no-op). Default: true.
116
+ *
117
+ * Named deviation: upstream reads this as `!!config.enabled`, so a config
118
+ * that never writes it has a logger that silently logs nothing. That only
119
+ * works because upstream's own generated config always writes `enabled:
120
+ * true` — a default that turns the feature off is a trap for every config
121
+ * written by hand.
122
+ */
70
123
  enabled?: boolean;
71
124
  /** Optional logger name (assigned by the manager when omitted). */
72
125
  name?: string;