@c9up/spectrum 0.1.11 → 0.1.13
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 +30 -7
- package/dist/Logger.d.ts +14 -4
- package/dist/Logger.d.ts.map +1 -1
- package/dist/Logger.js +83 -49
- package/dist/Logger.js.map +1 -1
- package/dist/LoggerManager.d.ts.map +1 -1
- package/dist/LoggerManager.js +7 -1
- package/dist/LoggerManager.js.map +1 -1
- package/dist/RustLogBridge.d.ts.map +1 -1
- package/dist/RustLogBridge.js +76 -16
- package/dist/RustLogBridge.js.map +1 -1
- package/dist/SpectrumProvider.d.ts +1 -0
- package/dist/SpectrumProvider.d.ts.map +1 -1
- package/dist/SpectrumProvider.js +30 -21
- package/dist/SpectrumProvider.js.map +1 -1
- package/dist/augmentations.d.ts +29 -0
- package/dist/augmentations.d.ts.map +1 -0
- package/dist/augmentations.js +18 -0
- package/dist/augmentations.js.map +1 -0
- package/dist/channels/FileChannel.d.ts +8 -0
- package/dist/channels/FileChannel.d.ts.map +1 -1
- package/dist/channels/FileChannel.js +79 -7
- package/dist/channels/FileChannel.js.map +1 -1
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +19 -7
- package/dist/configure.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/redact.d.ts +29 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +90 -0
- package/dist/redact.js.map +1 -0
- package/dist/targets.d.ts +6 -1
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +59 -5
- package/dist/targets.js.map +1 -1
- package/dist/types.d.ts +28 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +33 -6
- package/dist/types.js.map +1 -1
- package/package.json +4 -2
- package/src/Logger.ts +113 -51
- package/src/LoggerManager.ts +9 -1
- package/src/RustLogBridge.ts +95 -20
- package/src/SpectrumProvider.ts +34 -25
- package/src/augmentations.ts +32 -0
- package/src/channels/FileChannel.ts +80 -7
- package/src/configure.ts +19 -7
- package/src/index.ts +4 -0
- package/src/redact.ts +100 -0
- package/src/targets.ts +78 -12
- package/src/types.ts +50 -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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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:
|
|
14
|
-
debug:
|
|
15
|
-
info:
|
|
16
|
-
warn:
|
|
17
|
-
error:
|
|
18
|
-
fatal:
|
|
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;
|
|
@@ -66,7 +101,15 @@ export interface RedactOptions {
|
|
|
66
101
|
* / pino, with spectrum divergences called out inline.
|
|
67
102
|
*/
|
|
68
103
|
export interface LogConfig {
|
|
69
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* Turn the logger off while keeping its API callable (no-op). Default: true.
|
|
106
|
+
*
|
|
107
|
+
* Named deviation: upstream reads this as `!!config.enabled`, so a config
|
|
108
|
+
* that never writes it has a logger that silently logs nothing. That only
|
|
109
|
+
* works because upstream's own generated config always writes `enabled:
|
|
110
|
+
* true` — a default that turns the feature off is a trap for every config
|
|
111
|
+
* written by hand.
|
|
112
|
+
*/
|
|
70
113
|
enabled?: boolean;
|
|
71
114
|
/** Optional logger name (assigned by the manager when omitted). */
|
|
72
115
|
name?: string;
|