@c9up/spectrum 0.1.18 → 0.1.19

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 (85) hide show
  1. package/README.md +23 -6
  2. package/dist/Logger.d.ts +6 -2
  3. package/dist/Logger.d.ts.map +1 -1
  4. package/dist/Logger.js +190 -72
  5. package/dist/Logger.js.map +1 -1
  6. package/dist/LoggerManager.d.ts +5 -0
  7. package/dist/LoggerManager.d.ts.map +1 -1
  8. package/dist/LoggerManager.js +10 -3
  9. package/dist/LoggerManager.js.map +1 -1
  10. package/dist/RustLogBridge.d.ts +27 -2
  11. package/dist/RustLogBridge.d.ts.map +1 -1
  12. package/dist/RustLogBridge.js +45 -31
  13. package/dist/RustLogBridge.js.map +1 -1
  14. package/dist/SpectrumProvider.d.ts +6 -2
  15. package/dist/SpectrumProvider.d.ts.map +1 -1
  16. package/dist/SpectrumProvider.js +13 -11
  17. package/dist/SpectrumProvider.js.map +1 -1
  18. package/dist/channels/ConsoleChannel.d.ts +8 -1
  19. package/dist/channels/ConsoleChannel.d.ts.map +1 -1
  20. package/dist/channels/ConsoleChannel.js +18 -14
  21. package/dist/channels/ConsoleChannel.js.map +1 -1
  22. package/dist/channels/FileChannel.d.ts +32 -9
  23. package/dist/channels/FileChannel.d.ts.map +1 -1
  24. package/dist/channels/FileChannel.js +264 -202
  25. package/dist/channels/FileChannel.js.map +1 -1
  26. package/dist/errors.d.ts +9 -0
  27. package/dist/errors.d.ts.map +1 -0
  28. package/dist/errors.js +13 -0
  29. package/dist/errors.js.map +1 -0
  30. package/dist/index.d.ts +7 -4
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +4 -2
  33. package/dist/index.js.map +1 -1
  34. package/dist/jsonLine.d.ts +49 -0
  35. package/dist/jsonLine.d.ts.map +1 -0
  36. package/dist/jsonLine.js +164 -0
  37. package/dist/jsonLine.js.map +1 -0
  38. package/dist/redact.d.ts +16 -3
  39. package/dist/redact.d.ts.map +1 -1
  40. package/dist/redact.js +121 -10
  41. package/dist/redact.js.map +1 -1
  42. package/dist/sanitize.d.ts +11 -5
  43. package/dist/sanitize.d.ts.map +1 -1
  44. package/dist/sanitize.js +36 -12
  45. package/dist/sanitize.js.map +1 -1
  46. package/dist/services/main.d.ts.map +1 -1
  47. package/dist/services/main.js +12 -1
  48. package/dist/services/main.js.map +1 -1
  49. package/dist/stdSerializers.d.ts +47 -0
  50. package/dist/stdSerializers.d.ts.map +1 -0
  51. package/dist/stdSerializers.js +128 -0
  52. package/dist/stdSerializers.js.map +1 -0
  53. package/dist/stringify.d.ts +33 -0
  54. package/dist/stringify.d.ts.map +1 -0
  55. package/dist/stringify.js +148 -0
  56. package/dist/stringify.js.map +1 -0
  57. package/dist/targets.d.ts +29 -7
  58. package/dist/targets.d.ts.map +1 -1
  59. package/dist/targets.js +130 -31
  60. package/dist/targets.js.map +1 -1
  61. package/dist/testing/FakeLogger.js +20 -5
  62. package/dist/testing/FakeLogger.js.map +1 -1
  63. package/dist/types.d.ts +71 -3
  64. package/dist/types.d.ts.map +1 -1
  65. package/dist/types.js +18 -2
  66. package/dist/types.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/Logger.ts +254 -79
  69. package/src/LoggerManager.ts +10 -3
  70. package/src/RustLogBridge.ts +59 -30
  71. package/src/SpectrumProvider.ts +26 -17
  72. package/src/channels/ConsoleChannel.ts +30 -14
  73. package/src/channels/FileChannel.ts +319 -220
  74. package/src/errors.ts +13 -0
  75. package/src/index.ts +17 -3
  76. package/src/jsonLine.ts +222 -0
  77. package/src/redact.ts +133 -9
  78. package/src/sanitize.ts +36 -12
  79. package/src/services/main.ts +13 -1
  80. package/src/stdSerializers.ts +146 -0
  81. package/src/stringify.ts +175 -0
  82. package/src/targets.ts +193 -42
  83. package/src/testing/FakeLogger.ts +23 -5
  84. package/src/types.ts +97 -4
  85. package/stubs/config/logger.stub +1 -1
@@ -1,17 +1,18 @@
1
1
  import "./augmentations.js";
2
2
  import { ConsoleChannel } from "./channels/ConsoleChannel.js";
3
+ import { SpectrumError } from "./errors.js";
3
4
  import { onLog } from "./events.js";
4
5
  import { Logger } from "./Logger.js";
5
6
  import { LoggerManager } from "./LoggerManager.js";
6
7
  import { clearLogger, getLogger, setLogger } from "./services/main.js";
7
- import { channelsFromTargets } from "./targets.js";
8
+ import { channelsFromConfig } from "./targets.js";
8
9
  import type {
9
10
  LogChannel,
10
11
  LogConfig,
11
12
  LoggerManagerConfig,
12
13
  LogLevelWithSilent,
13
14
  } from "./types.js";
14
- import { isLogLevelWithSilent, logLevel } from "./types.js";
15
+ import { assertLogLevel, logLevel } from "./types.js";
15
16
 
16
17
  /** An emitter, as far as this provider is concerned. Structural: no import. */
17
18
  function hasEmit(
@@ -45,13 +46,17 @@ function isPartialLogConfig(value: unknown): value is Partial<LogConfig> {
45
46
  return typeof value === "object" && value !== null;
46
47
  }
47
48
 
49
+ /**
50
+ * The slice of a container the provider uses. Answers `unknown` rather than a
51
+ * caller-chosen `T`: what comes back is checked, not asserted.
52
+ */
48
53
  interface SpectrumContainer {
49
54
  singleton(token: unknown, factory: () => unknown): void;
50
- resolve<T = unknown>(token: unknown): Promise<T>;
55
+ resolve(token: unknown): Promise<unknown>;
51
56
  }
52
57
 
53
58
  interface SpectrumConfigStore {
54
- get<T = unknown>(key: string): T | undefined;
59
+ get(key: string): unknown;
55
60
  }
56
61
 
57
62
  export interface SpectrumAppContext {
@@ -68,7 +73,7 @@ export default class SpectrumProvider {
68
73
  constructor(protected app: SpectrumAppContext) {}
69
74
 
70
75
  register() {
71
- const raw = this.app.config.get<unknown>("logger");
76
+ const raw = this.app.config.get("logger");
72
77
  const managerConfig = this.#normalize(raw);
73
78
 
74
79
  // Collect every channel across all loggers so shutdown can release them.
@@ -83,14 +88,19 @@ export default class SpectrumProvider {
83
88
  // token stays bound beside it: it is what every existing
84
89
  // `container.make(...)` asks for, and a token is not worth breaking an
85
90
  // application over.
86
- const logger = (): Promise<Logger> =>
87
- this.app.container.resolve<Logger>(Logger);
91
+ const logger = (): Promise<unknown> => this.app.container.resolve(Logger);
88
92
  this.app.container.singleton("spectrum.logger", logger);
89
93
  this.app.container.singleton("logger", logger);
90
94
  }
91
95
 
92
96
  async boot() {
93
- const logger = await this.app.container.resolve<Logger>(Logger);
97
+ const logger = await this.app.container.resolve(Logger);
98
+ if (!(logger instanceof Logger)) {
99
+ throw new SpectrumError(
100
+ "E_SPECTRUM_LOGGER_NOT_BOUND",
101
+ "the container resolved something other than a Logger for the Logger token",
102
+ );
103
+ }
94
104
  this.#owned = logger;
95
105
  setLogger(logger);
96
106
  await this.#bridgeLogEvents();
@@ -189,9 +199,12 @@ export default class SpectrumProvider {
189
199
  }
190
200
 
191
201
  #normalizeLogger(cfg: Partial<LogConfig>): LogConfig {
192
- const level = isLogLevelWithSilent(cfg.level)
193
- ? cfg.level
194
- : logLevel(process.env.LOG_LEVEL);
202
+ // An invalid level in the config is a config error, not a cue to take
203
+ // whatever the environment says instead.
204
+ const level =
205
+ cfg.level === undefined
206
+ ? logLevel(process.env.LOG_LEVEL)
207
+ : assertLogLevel(cfg.level);
195
208
  // An AdonisJS `config/logger.ts` declares its output as
196
209
  // `transport.targets`, not as channels. It has to be converted HERE:
197
210
  // filling `channels` in with the console fallback below is what stopped
@@ -202,16 +215,12 @@ export default class SpectrumProvider {
202
215
  return { ...cfg, level, channels };
203
216
  }
204
217
 
205
- /** Explicit channels, else the declared targets, else a pretty console. */
218
+ /** Explicit channels, else the declared destination or targets, else a pretty console. */
206
219
  #channelsFor(
207
220
  cfg: Partial<LogConfig>,
208
221
  level: LogLevelWithSilent,
209
222
  ): LogChannel[] {
210
223
  if (cfg.channels && cfg.channels.length > 0) return cfg.channels;
211
- const declared = cfg.transport?.targets;
212
- if (declared && declared.length > 0) {
213
- return channelsFromTargets(declared, level);
214
- }
215
- return [new ConsoleChannel("pretty")];
224
+ return channelsFromConfig(cfg, level) ?? [new ConsoleChannel("pretty")];
216
225
  }
217
226
  }
@@ -4,16 +4,31 @@
4
4
  * @implements FR57, FR58
5
5
  */
6
6
 
7
+ import { jsonLine, lineOptionsFor } from "../jsonLine.js";
7
8
  import { sanitizeLogValue } from "../sanitize.js";
9
+ import { stringifyRecord } from "../stringify.js";
8
10
  import type { LogChannel, LogEntry } from "../types.js";
9
11
  import { logEntryExtras } from "../types.js";
10
12
 
13
+ export interface ConsoleChannelOptions {
14
+ /**
15
+ * Write every level to this one stream. Without it, error and fatal go to
16
+ * stderr and the rest to stdout.
17
+ */
18
+ stream?: "stdout" | "stderr";
19
+ }
20
+
11
21
  export class ConsoleChannel implements LogChannel {
12
22
  name = "console";
13
23
  #format: "pretty" | "json";
24
+ readonly #stream: "stdout" | "stderr" | undefined;
14
25
 
15
- constructor(format: "pretty" | "json" = "pretty") {
26
+ constructor(
27
+ format: "pretty" | "json" = "pretty",
28
+ options: ConsoleChannelOptions = {},
29
+ ) {
16
30
  this.#format = format;
31
+ this.#stream = options.stream;
17
32
  }
18
33
 
19
34
  write(entry: LogEntry): void {
@@ -27,15 +42,7 @@ export class ConsoleChannel implements LogChannel {
27
42
  #writeJson(entry: LogEntry): void {
28
43
  // Structural fields first, then merged root fields spread flat (pino
29
44
  // output parity). Legacy `data` (if present) stays under its own key.
30
- const output = JSON.stringify({
31
- timestamp: entry.timestamp,
32
- level: entry.level,
33
- module: entry.module,
34
- message: entry.message,
35
- correlationId: entry.correlationId,
36
- data: entry.data,
37
- ...logEntryExtras(entry),
38
- });
45
+ const output = jsonLine(entry);
39
46
  this.#writeToStream(entry.level, `${output}\n`);
40
47
  }
41
48
 
@@ -58,11 +65,14 @@ export class ConsoleChannel implements LogChannel {
58
65
  : "";
59
66
  const cid = cidRaw ? ` cid=${this.#sanitize(cidRaw)}` : "";
60
67
  // Prefer legacy `data`; otherwise render the merged root fields. JSON
61
- // encoding escapes control chars, so no CRLF can leak into the line.
68
+ // escapes C0 controls but not DEL, C1 or U+2028/2029, so the rendered
69
+ // data goes through the same sanitizer as the message.
62
70
  const extras = logEntryExtras(entry);
63
71
  const payload =
64
72
  entry.data ?? (Object.keys(extras).length > 0 ? extras : undefined);
65
- const dataStr = payload ? ` ${JSON.stringify(payload)}` : "";
73
+ const dataStr = payload
74
+ ? ` ${this.#sanitize(stringifyRecord(payload, lineOptionsFor(entry).limits))}`
75
+ : "";
66
76
  const message = this.#sanitize(entry.message);
67
77
  const module = this.#sanitize(entry.module);
68
78
 
@@ -72,9 +82,15 @@ export class ConsoleChannel implements LogChannel {
72
82
  );
73
83
  }
74
84
 
75
- /** Route error/fatal to stderr, others to stdout. */
85
+ /** The configured stream, else error/fatal to stderr and others to stdout. */
76
86
  #writeToStream(level: string, output: string): void {
77
- if (level === "error" || level === "fatal") {
87
+ if (this.#stream === "stdout") {
88
+ process.stdout.write(output);
89
+ } else if (
90
+ this.#stream === "stderr" ||
91
+ level === "error" ||
92
+ level === "fatal"
93
+ ) {
78
94
  process.stderr.write(output);
79
95
  } else {
80
96
  process.stdout.write(output);