@c9up/spectrum 0.1.18 → 0.1.20
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.
- package/README.md +33 -7
- package/dist/Logger.d.ts +6 -2
- package/dist/Logger.d.ts.map +1 -1
- package/dist/Logger.js +214 -80
- package/dist/Logger.js.map +1 -1
- package/dist/LoggerManager.d.ts +5 -0
- package/dist/LoggerManager.d.ts.map +1 -1
- package/dist/LoggerManager.js +10 -3
- package/dist/LoggerManager.js.map +1 -1
- package/dist/RustLogBridge.d.ts +27 -2
- package/dist/RustLogBridge.d.ts.map +1 -1
- package/dist/RustLogBridge.js +45 -31
- package/dist/RustLogBridge.js.map +1 -1
- package/dist/SpectrumProvider.d.ts +6 -2
- package/dist/SpectrumProvider.d.ts.map +1 -1
- package/dist/SpectrumProvider.js +18 -12
- package/dist/SpectrumProvider.js.map +1 -1
- package/dist/channels/ConsoleChannel.d.ts +8 -1
- package/dist/channels/ConsoleChannel.d.ts.map +1 -1
- package/dist/channels/ConsoleChannel.js +18 -14
- package/dist/channels/ConsoleChannel.js.map +1 -1
- package/dist/channels/FileChannel.d.ts +32 -9
- package/dist/channels/FileChannel.d.ts.map +1 -1
- package/dist/channels/FileChannel.js +264 -202
- package/dist/channels/FileChannel.js.map +1 -1
- package/dist/errors.d.ts +9 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/jsonLine.d.ts +49 -0
- package/dist/jsonLine.d.ts.map +1 -0
- package/dist/jsonLine.js +165 -0
- package/dist/jsonLine.js.map +1 -0
- package/dist/redact.d.ts +16 -3
- package/dist/redact.d.ts.map +1 -1
- package/dist/redact.js +121 -10
- package/dist/redact.js.map +1 -1
- package/dist/sanitize.d.ts +11 -5
- package/dist/sanitize.d.ts.map +1 -1
- package/dist/sanitize.js +36 -12
- package/dist/sanitize.js.map +1 -1
- package/dist/services/main.d.ts.map +1 -1
- package/dist/services/main.js +12 -1
- package/dist/services/main.js.map +1 -1
- package/dist/stdSerializers.d.ts +48 -0
- package/dist/stdSerializers.d.ts.map +1 -0
- package/dist/stdSerializers.js +129 -0
- package/dist/stdSerializers.js.map +1 -0
- package/dist/stringify.d.ts +33 -0
- package/dist/stringify.d.ts.map +1 -0
- package/dist/stringify.js +148 -0
- package/dist/stringify.js.map +1 -0
- package/dist/targets.d.ts +29 -7
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +130 -31
- package/dist/targets.js.map +1 -1
- package/dist/testing/FakeLogger.js +20 -5
- package/dist/testing/FakeLogger.js.map +1 -1
- package/dist/types.d.ts +71 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +21 -3
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/Logger.ts +277 -85
- package/src/LoggerManager.ts +10 -3
- package/src/RustLogBridge.ts +59 -30
- package/src/SpectrumProvider.ts +31 -18
- package/src/channels/ConsoleChannel.ts +30 -14
- package/src/channels/FileChannel.ts +319 -220
- package/src/errors.ts +13 -0
- package/src/index.ts +17 -3
- package/src/jsonLine.ts +223 -0
- package/src/redact.ts +133 -9
- package/src/sanitize.ts +36 -12
- package/src/services/main.ts +13 -1
- package/src/stdSerializers.ts +147 -0
- package/src/stringify.ts +175 -0
- package/src/targets.ts +193 -42
- package/src/testing/FakeLogger.ts +23 -5
- package/src/types.ts +100 -5
- package/stubs/config/logger.stub +11 -1
package/src/SpectrumProvider.ts
CHANGED
|
@@ -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 {
|
|
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 {
|
|
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
|
|
55
|
+
resolve(token: unknown): Promise<unknown>;
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
interface SpectrumConfigStore {
|
|
54
|
-
get
|
|
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
|
|
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<
|
|
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
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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,16 @@ export default class SpectrumProvider {
|
|
|
202
215
|
return { ...cfg, level, channels };
|
|
203
216
|
}
|
|
204
217
|
|
|
205
|
-
/**
|
|
218
|
+
/**
|
|
219
|
+
* Explicit channels, else the declared destination or targets, else a pretty
|
|
220
|
+
* console. An explicit `[]` is kept: it is how a logger is silenced, and the
|
|
221
|
+
* Logger reads it the same way — only a missing `channels` takes a default.
|
|
222
|
+
*/
|
|
206
223
|
#channelsFor(
|
|
207
224
|
cfg: Partial<LogConfig>,
|
|
208
225
|
level: LogLevelWithSilent,
|
|
209
226
|
): LogChannel[] {
|
|
210
|
-
if (cfg.channels
|
|
211
|
-
|
|
212
|
-
if (declared && declared.length > 0) {
|
|
213
|
-
return channelsFromTargets(declared, level);
|
|
214
|
-
}
|
|
215
|
-
return [new ConsoleChannel("pretty")];
|
|
227
|
+
if (cfg.channels !== undefined) return cfg.channels;
|
|
228
|
+
return channelsFromConfig(cfg, level) ?? [new ConsoleChannel("pretty")];
|
|
216
229
|
}
|
|
217
230
|
}
|
|
@@ -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(
|
|
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 =
|
|
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
|
-
//
|
|
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
|
|
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
|
-
/**
|
|
85
|
+
/** The configured stream, else error/fatal to stderr and others to stdout. */
|
|
76
86
|
#writeToStream(level: string, output: string): void {
|
|
77
|
-
if (
|
|
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);
|