@c9up/spectrum 0.1.17 → 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 (90) 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 +195 -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 +51 -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/events.d.ts +38 -0
  31. package/dist/events.d.ts.map +1 -0
  32. package/dist/events.js +55 -0
  33. package/dist/events.js.map +1 -0
  34. package/dist/index.d.ts +8 -4
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +5 -2
  37. package/dist/index.js.map +1 -1
  38. package/dist/jsonLine.d.ts +49 -0
  39. package/dist/jsonLine.d.ts.map +1 -0
  40. package/dist/jsonLine.js +164 -0
  41. package/dist/jsonLine.js.map +1 -0
  42. package/dist/redact.d.ts +16 -3
  43. package/dist/redact.d.ts.map +1 -1
  44. package/dist/redact.js +121 -10
  45. package/dist/redact.js.map +1 -1
  46. package/dist/sanitize.d.ts +11 -5
  47. package/dist/sanitize.d.ts.map +1 -1
  48. package/dist/sanitize.js +36 -12
  49. package/dist/sanitize.js.map +1 -1
  50. package/dist/services/main.d.ts.map +1 -1
  51. package/dist/services/main.js +12 -1
  52. package/dist/services/main.js.map +1 -1
  53. package/dist/stdSerializers.d.ts +47 -0
  54. package/dist/stdSerializers.d.ts.map +1 -0
  55. package/dist/stdSerializers.js +128 -0
  56. package/dist/stdSerializers.js.map +1 -0
  57. package/dist/stringify.d.ts +33 -0
  58. package/dist/stringify.d.ts.map +1 -0
  59. package/dist/stringify.js +148 -0
  60. package/dist/stringify.js.map +1 -0
  61. package/dist/targets.d.ts +29 -7
  62. package/dist/targets.d.ts.map +1 -1
  63. package/dist/targets.js +130 -31
  64. package/dist/targets.js.map +1 -1
  65. package/dist/testing/FakeLogger.js +20 -5
  66. package/dist/testing/FakeLogger.js.map +1 -1
  67. package/dist/types.d.ts +71 -3
  68. package/dist/types.d.ts.map +1 -1
  69. package/dist/types.js +18 -2
  70. package/dist/types.js.map +1 -1
  71. package/package.json +1 -1
  72. package/src/Logger.ts +259 -79
  73. package/src/LoggerManager.ts +10 -3
  74. package/src/RustLogBridge.ts +59 -30
  75. package/src/SpectrumProvider.ts +69 -17
  76. package/src/channels/ConsoleChannel.ts +30 -14
  77. package/src/channels/FileChannel.ts +319 -220
  78. package/src/errors.ts +13 -0
  79. package/src/events.ts +62 -0
  80. package/src/index.ts +18 -3
  81. package/src/jsonLine.ts +222 -0
  82. package/src/redact.ts +133 -9
  83. package/src/sanitize.ts +36 -12
  84. package/src/services/main.ts +13 -1
  85. package/src/stdSerializers.ts +146 -0
  86. package/src/stringify.ts +175 -0
  87. package/src/targets.ts +193 -42
  88. package/src/testing/FakeLogger.ts +23 -5
  89. package/src/types.ts +97 -4
  90. package/stubs/config/logger.stub +1 -1
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { SpectrumError } from "./errors.js";
2
+ import type { LineFormatters, TimestampOption } from "./jsonLine.js";
1
3
  import type { TransportTargetOptions } from "./targets.js";
2
4
  /**
3
5
  * Spectrum types.
@@ -46,9 +48,25 @@ export function isLogLevelWithSilent(
46
48
  return typeof value === "string" && CONFIGURED_LEVELS.has(value);
47
49
  }
48
50
 
51
+ /**
52
+ * A configured level, checked.
53
+ *
54
+ * An unknown level is a config error, as it is upstream ("unknown level"):
55
+ * reading `ERROR` or `warning` as `info` meant an operator quieting production
56
+ * got more output, and no word about it.
57
+ */
58
+ export function assertLogLevel(value: unknown): LogLevelWithSilent {
59
+ if (isLogLevelWithSilent(value)) return value;
60
+ throw new SpectrumError(
61
+ "E_SPECTRUM_UNKNOWN_LEVEL",
62
+ `unknown level ${JSON.stringify(value) ?? String(value)}. Use one of: ${[...CONFIGURED_LEVELS].join(", ")}.`,
63
+ );
64
+ }
65
+
49
66
  /**
50
67
  * Narrow an untyped level — `process.env.LOG_LEVEL`, a CLI flag — to one the
51
- * config can name, falling back to `info`.
68
+ * config can name. Absent (or empty) falls back to `fallback`; set to
69
+ * something that is no level, it throws.
52
70
  *
53
71
  * Without it a config file has to assert the env string into the union, and an
54
72
  * assertion is the wrong tool for a value that comes from outside the program:
@@ -58,7 +76,8 @@ export function logLevel(
58
76
  raw: unknown,
59
77
  fallback: LogLevelWithSilent = "info",
60
78
  ): LogLevelWithSilent {
61
- return isLogLevelWithSilent(raw) ? raw : fallback;
79
+ if (raw === undefined || raw === "") return fallback;
80
+ return assertLogLevel(raw);
62
81
  }
63
82
 
64
83
  export interface LogEntry {
@@ -103,7 +122,44 @@ export type LogSerializer = (value: unknown) => unknown;
103
122
  /** Redaction config (pino `redact` parity). */
104
123
  export interface RedactOptions {
105
124
  paths: string[];
106
- censor?: string;
125
+ /**
126
+ * What a matched value becomes. A function is called with the value and
127
+ * the path it was found at (wildcards resolved). Default `[Redacted]`.
128
+ */
129
+ censor?: string | ((value: unknown, path: string[]) => unknown);
130
+ /** Remove matched keys instead of censoring them. */
131
+ remove?: boolean;
132
+ }
133
+
134
+ /** Called with each log call's arguments; calls `method` to write (pino `hooks.logMethod`). */
135
+ export type LogMethodHook = (
136
+ this: unknown,
137
+ args: unknown[],
138
+ method: (...args: unknown[]) => void,
139
+ level: number,
140
+ ) => void;
141
+
142
+ /** Fields to add to every line, computed per call (pino `mixin`). */
143
+ export type LogMixin = (
144
+ mergeObject: Record<string, unknown>,
145
+ level: number,
146
+ logger: unknown,
147
+ ) => Record<string, unknown>;
148
+
149
+ /** Options of `logger.child(bindings, options)`. */
150
+ export interface ChildOptions {
151
+ level?: LogLevelWithSilent;
152
+ /** Replaces the parent's redaction for this child. */
153
+ redact?: string[] | RedactOptions;
154
+ /** Added to (and overriding) the parent's serializers. */
155
+ serializers?: Record<string, LogSerializer>;
156
+ /** Appended to the parent's prefix, written before every message. */
157
+ msgPrefix?: string;
158
+ }
159
+
160
+ /** Anything with a `write(line)` method, used as a JSON destination. */
161
+ export interface LineWritable {
162
+ write(line: string): unknown;
107
163
  }
108
164
 
109
165
  /**
@@ -132,13 +188,50 @@ export interface LogConfig {
132
188
  * drives real output instead of being quietly ignored. Explicit `channels`
133
189
  * win when both are given.
134
190
  */
135
- transport?: { targets?: readonly TransportTargetOptions[] };
191
+ transport?:
192
+ | { targets?: readonly TransportTargetOptions[] }
193
+ | TransportTargetOptions;
194
+ /**
195
+ * Where JSON lines go when no `channels` are given: a file path, a file
196
+ * descriptor, or anything with a `write(line)` method. Wins over
197
+ * `transport`, as upstream.
198
+ */
199
+ destination?: string | number | LineWritable;
136
200
  /** Per-module level overrides — spectrum divergence. */
137
201
  modules?: Record<string, LogLevelWithSilent>;
138
202
  /** Keys / dot-paths to redact on the merging object before writing. */
139
203
  redact?: string[] | RedactOptions;
140
204
  /** Per-key value serializers (`err`/`error` handled by default). */
141
205
  serializers?: Record<string, LogSerializer>;
206
+ /**
207
+ * How deep a value JSON cannot write (a cycle, a BigInt) is walked before
208
+ * `"[Object]"` stands in for the rest. Default 5, as pino.
209
+ */
210
+ depthLimit?: number;
211
+ /** How many entries of one such object or array are written. Default 100, as pino. */
212
+ edgeLimit?: number;
213
+ /** The key a JSON line carries the message under. Default `msg`. */
214
+ messageKey?: string;
215
+ /** How a JSON line writes `time`. Default `'epoch'` (milliseconds). */
216
+ timestamp?: TimestampOption;
217
+ /** Fields on every JSON line. Default `{ pid, hostname }`; `null` for none. */
218
+ base?: Record<string, unknown> | null;
219
+ /** Reshape the level part and the merged fields of a JSON line. */
220
+ formatters?: LineFormatters;
221
+ /** The key an Error passed to a log call is written under. Default `err`. */
222
+ errorKey?: string;
223
+ /** Written before every message. */
224
+ msgPrefix?: string;
225
+ /** Fields added to every line, computed per call. A merging object wins over them. */
226
+ mixin?: LogMixin;
227
+ /** How the mixin's fields and the merging object combine. Default: merging object wins. */
228
+ mixinMergeStrategy?: (
229
+ mergeObject: Record<string, unknown>,
230
+ mixinObject: Record<string, unknown>,
231
+ ) => Record<string, unknown>;
232
+ hooks?: { logMethod?: LogMethodHook };
233
+ /** Called with every child logger created. */
234
+ onChild?: (child: unknown) => void;
142
235
  }
143
236
 
144
237
  /**
@@ -12,7 +12,7 @@ export default defineConfig({
12
12
  enabled: true,
13
13
  name: process.env.APP_NAME ?? 'app',
14
14
  // LOG_LEVEL comes from outside the program, so it is checked rather than
15
- // asserted: anything that is not a level reads as 'info'.
15
+ // asserted: unset reads as 'info', and a value that is no level throws.
16
16
  level: logLevel(process.env.LOG_LEVEL),
17
17
  transport: {
18
18
  targets: targets()