@c9up/spectrum 0.1.12 → 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.map +1 -1
- package/dist/channels/FileChannel.js +26 -6
- 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 +30 -6
- 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/SpectrumProvider.ts
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
|
+
import "./augmentations.js";
|
|
1
2
|
import { ConsoleChannel } from "./channels/ConsoleChannel.js";
|
|
2
3
|
import { Logger } from "./Logger.js";
|
|
3
4
|
import { LoggerManager } from "./LoggerManager.js";
|
|
4
5
|
import { setLogger } from "./services/main.js";
|
|
6
|
+
import { channelsFromTargets } from "./targets.js";
|
|
5
7
|
import type {
|
|
6
8
|
LogChannel,
|
|
7
9
|
LogConfig,
|
|
8
10
|
LoggerManagerConfig,
|
|
9
11
|
LogLevelWithSilent,
|
|
10
12
|
} from "./types.js";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
const VALID_LEVELS = new Set<string>([
|
|
14
|
-
...Object.keys(LOG_LEVEL_ORDER),
|
|
15
|
-
"silent",
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
function isValidLevel(raw: unknown): raw is LogLevelWithSilent {
|
|
19
|
-
return typeof raw === "string" && VALID_LEVELS.has(raw);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function resolveLogLevel(raw: unknown): LogLevelWithSilent {
|
|
23
|
-
return isValidLevel(raw) ? raw : "info";
|
|
24
|
-
}
|
|
13
|
+
import { isLogLevelWithSilent, logLevel } from "./types.js";
|
|
25
14
|
|
|
26
15
|
/** A channel that owns a resource (e.g. a FileChannel WriteStream) to release on shutdown. */
|
|
27
16
|
function hasClose(ch: LogChannel): ch is LogChannel & { close(): void } {
|
|
@@ -71,9 +60,15 @@ export default class SpectrumProvider {
|
|
|
71
60
|
|
|
72
61
|
const manager = new LoggerManager(managerConfig);
|
|
73
62
|
this.app.container.singleton(Logger, () => manager);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
63
|
+
// Namespaced by the package that owns it, the way upstream namespaces
|
|
64
|
+
// `lucid.db`, `auth.manager` and `drive.manager` by theirs. The bare
|
|
65
|
+
// token stays bound beside it: it is what every existing
|
|
66
|
+
// `container.make(...)` asks for, and a token is not worth breaking an
|
|
67
|
+
// application over.
|
|
68
|
+
const logger = (): Promise<Logger> =>
|
|
69
|
+
this.app.container.resolve<Logger>(Logger);
|
|
70
|
+
this.app.container.singleton("spectrum.logger", logger);
|
|
71
|
+
this.app.container.singleton("logger", logger);
|
|
77
72
|
}
|
|
78
73
|
|
|
79
74
|
async boot() {
|
|
@@ -113,15 +108,29 @@ export default class SpectrumProvider {
|
|
|
113
108
|
}
|
|
114
109
|
|
|
115
110
|
#normalizeLogger(cfg: Partial<LogConfig>): LogConfig {
|
|
116
|
-
|
|
117
|
-
// only when none are supplied.
|
|
118
|
-
const channels =
|
|
119
|
-
cfg.channels && cfg.channels.length > 0
|
|
120
|
-
? cfg.channels
|
|
121
|
-
: [new ConsoleChannel("pretty")];
|
|
122
|
-
const level = isValidLevel(cfg.level)
|
|
111
|
+
const level = isLogLevelWithSilent(cfg.level)
|
|
123
112
|
? cfg.level
|
|
124
|
-
:
|
|
113
|
+
: logLevel(process.env.LOG_LEVEL);
|
|
114
|
+
// An AdonisJS `config/logger.ts` declares its output as
|
|
115
|
+
// `transport.targets`, not as channels. It has to be converted HERE:
|
|
116
|
+
// filling `channels` in with the console fallback below is what stopped
|
|
117
|
+
// the Logger's own conversion from ever running (it only fires when
|
|
118
|
+
// `channels` is absent), so a config that declared a file target reached
|
|
119
|
+
// production writing to a console and nothing to the file it named.
|
|
120
|
+
const channels = this.#channelsFor(cfg, level);
|
|
125
121
|
return { ...cfg, level, channels };
|
|
126
122
|
}
|
|
123
|
+
|
|
124
|
+
/** Explicit channels, else the declared targets, else a pretty console. */
|
|
125
|
+
#channelsFor(
|
|
126
|
+
cfg: Partial<LogConfig>,
|
|
127
|
+
level: LogLevelWithSilent,
|
|
128
|
+
): LogChannel[] {
|
|
129
|
+
if (cfg.channels && cfg.channels.length > 0) return cfg.channels;
|
|
130
|
+
const declared = cfg.transport?.targets;
|
|
131
|
+
if (declared && declared.length > 0) {
|
|
132
|
+
return channelsFromTargets(declared, level);
|
|
133
|
+
}
|
|
134
|
+
return [new ConsoleChannel("pretty")];
|
|
135
|
+
}
|
|
127
136
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Teach ream's `ContainerBindings` what `container.make('logger')` returns.
|
|
3
|
+
*
|
|
4
|
+
* ream declares that interface open on purpose: it registers its own entries
|
|
5
|
+
* and expects each package to contribute the one it owns — the comment on the
|
|
6
|
+
* interface names `logger` (spectrum) as exactly this. Nothing filled it in,
|
|
7
|
+
* so resolving by the string token answered `unknown` and every call site had
|
|
8
|
+
* to assert a type it could not prove.
|
|
9
|
+
*
|
|
10
|
+
* Loaded from the package barrel and from the provider, so registering
|
|
11
|
+
* spectrum is enough — an application writes no `declare module` of its own.
|
|
12
|
+
*
|
|
13
|
+
* Type-only, and ream stays an OPTIONAL peer: nothing here reaches a runtime
|
|
14
|
+
* import, and a `declare module` for a specifier that does not resolve is
|
|
15
|
+
* simply inert.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Referenced so the augmentation below resolves the module it augments.
|
|
19
|
+
import type {} from "@c9up/ream/types";
|
|
20
|
+
import type { Logger } from "./Logger.js";
|
|
21
|
+
|
|
22
|
+
declare module "@c9up/ream/types" {
|
|
23
|
+
interface ContainerBindings {
|
|
24
|
+
/** The logger, bound by `SpectrumProvider`. */
|
|
25
|
+
"spectrum.logger": Logger;
|
|
26
|
+
/**
|
|
27
|
+
* The same binding under the name it had before the token carried its
|
|
28
|
+
* package. Kept bound so an existing `container.make(...)` resolves.
|
|
29
|
+
*/
|
|
30
|
+
logger: Logger;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -76,7 +76,7 @@ export class FileChannel implements LogChannel {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
if (this.#
|
|
79
|
+
if (this.#needsRotation(bytes)) {
|
|
80
80
|
this.#pending.push(line);
|
|
81
81
|
this.#scheduleRotation();
|
|
82
82
|
return;
|
|
@@ -111,6 +111,23 @@ export class FileChannel implements LogChannel {
|
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Whether this line has to wait for a rotation.
|
|
116
|
+
*
|
|
117
|
+
* The empty-file test is what stops an entry LARGER than `maxSizeBytes`
|
|
118
|
+
* from spinning forever: a fresh file starts at zero, so `0 + bytes > max`
|
|
119
|
+
* was still true and the line went back on the buffer, scheduled another
|
|
120
|
+
* rotation, and came back to the same answer — churning through a
|
|
121
|
+
* generation each time and never being written.
|
|
122
|
+
*
|
|
123
|
+
* A log line cannot be split, so an oversized one is written whole into a
|
|
124
|
+
* file of its own and the next line rotates. Bigger than the limit once
|
|
125
|
+
* beats absent.
|
|
126
|
+
*/
|
|
127
|
+
#needsRotation(bytes: number): boolean {
|
|
128
|
+
return this.#currentSize > 0 && this.#currentSize + bytes > this.#maxSize;
|
|
129
|
+
}
|
|
130
|
+
|
|
114
131
|
#scheduleRotation(): void {
|
|
115
132
|
if (this.#rotating || this.#closed) return;
|
|
116
133
|
this.#rotating = true;
|
|
@@ -129,13 +146,20 @@ export class FileChannel implements LogChannel {
|
|
|
129
146
|
const oldest = `${this.#filePath}.${this.#maxFiles - 1}`;
|
|
130
147
|
await fsp.rm(oldest, { force: true });
|
|
131
148
|
|
|
149
|
+
// Shift the numbered generations up, oldest first: .1 → .2, .2 → .3…
|
|
150
|
+
//
|
|
151
|
+
// The i===1 pass used to take the LIVE file rather than `.1`, so `.1`
|
|
152
|
+
// was never written and the rename below found nothing left to move.
|
|
153
|
+
// With maxFiles=3 that kept one generation where two were configured,
|
|
154
|
+
// and every rotation destroyed the one before it.
|
|
132
155
|
for (let i = this.#maxFiles - 2; i >= 1; i--) {
|
|
133
|
-
const from = i === 1 ? this.#filePath : `${this.#filePath}.${i}`;
|
|
134
|
-
const to = `${this.#filePath}.${i + 1}`;
|
|
135
156
|
try {
|
|
136
|
-
await fsp.rename(
|
|
157
|
+
await fsp.rename(
|
|
158
|
+
`${this.#filePath}.${i}`,
|
|
159
|
+
`${this.#filePath}.${i + 1}`,
|
|
160
|
+
);
|
|
137
161
|
} catch {
|
|
138
|
-
//
|
|
162
|
+
// That generation does not exist yet — skip
|
|
139
163
|
}
|
|
140
164
|
}
|
|
141
165
|
|
|
@@ -164,7 +188,7 @@ export class FileChannel implements LogChannel {
|
|
|
164
188
|
const line = this.#pending.shift();
|
|
165
189
|
if (!line) continue;
|
|
166
190
|
const bytes = Buffer.byteLength(line, "utf8");
|
|
167
|
-
if (this.#
|
|
191
|
+
if (this.#needsRotation(bytes)) {
|
|
168
192
|
this.#pending.unshift(line);
|
|
169
193
|
this.#scheduleRotation();
|
|
170
194
|
return;
|
package/src/configure.ts
CHANGED
|
@@ -12,15 +12,27 @@ export async function configure(codemods: Codemods): Promise<void> {
|
|
|
12
12
|
await codemods.addProvider("@c9up/spectrum/provider");
|
|
13
13
|
await codemods.writeFile(
|
|
14
14
|
"config/logger.ts",
|
|
15
|
-
`import {
|
|
15
|
+
`import { defineConfig, logLevel, targets } from '@c9up/spectrum'
|
|
16
|
+
|
|
17
|
+
const inProduction = process.env.NODE_ENV === 'production'
|
|
16
18
|
|
|
17
19
|
export default defineConfig({
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
default: 'app',
|
|
21
|
+
loggers: {
|
|
22
|
+
app: {
|
|
23
|
+
enabled: true,
|
|
24
|
+
name: process.env.APP_NAME ?? 'app',
|
|
25
|
+
// LOG_LEVEL comes from outside the program, so it is checked rather than
|
|
26
|
+
// asserted: anything that is not a level reads as 'info'.
|
|
27
|
+
level: logLevel(process.env.LOG_LEVEL),
|
|
28
|
+
transport: {
|
|
29
|
+
targets: targets()
|
|
30
|
+
.pushIf(!inProduction, targets.pretty())
|
|
31
|
+
.pushIf(inProduction, targets.file({ destination: 1 }))
|
|
32
|
+
.toArray(),
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
24
36
|
})
|
|
25
37
|
`,
|
|
26
38
|
);
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @implements FR54, FR55, FR56, FR57, FR58
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import "./augmentations.js";
|
|
8
|
+
|
|
7
9
|
export { ConsoleChannel } from "./channels/ConsoleChannel.js";
|
|
8
10
|
export { FileChannel } from "./channels/FileChannel.js";
|
|
9
11
|
export { defineConfig } from "./config.js";
|
|
@@ -18,6 +20,7 @@ export type {
|
|
|
18
20
|
} from "./targets.js";
|
|
19
21
|
export { channelsFromTargets, Targets, targets } from "./targets.js";
|
|
20
22
|
export {
|
|
23
|
+
isLogLevelWithSilent,
|
|
21
24
|
type LogChannel,
|
|
22
25
|
type LogConfig,
|
|
23
26
|
type LogEntry,
|
|
@@ -25,5 +28,6 @@ export {
|
|
|
25
28
|
type LogLevelWithSilent,
|
|
26
29
|
type LogSerializer,
|
|
27
30
|
logEntryExtras,
|
|
31
|
+
logLevel,
|
|
28
32
|
type RedactOptions,
|
|
29
33
|
} from "./types.js";
|
package/src/redact.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction paths, in the syntax pino's `redact` option is written in.
|
|
3
|
+
*
|
|
4
|
+
* Upstream hands those paths to `fast-redact`, so a migrated `config/logger.ts`
|
|
5
|
+
* legitimately contains `req.headers.authorization`, `*.password`,
|
|
6
|
+
* `users[*].token` and `headers["set-cookie"]`. Understanding only the first of
|
|
7
|
+
* those — a literal dot-path — is not a smaller feature: it is a redaction that
|
|
8
|
+
* silently does nothing. The config says the password is censored, and the
|
|
9
|
+
* password is written out in full.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* fast-redact's own tokenizer: a run of characters that is not `.`, `[` or
|
|
14
|
+
* `]`, or anything between a pair of brackets.
|
|
15
|
+
*/
|
|
16
|
+
const PATH_TOKEN = /[^.[\]]+|\[(?:.)*?\]/g;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Split a redaction path into the keys it names.
|
|
20
|
+
*
|
|
21
|
+
* `a["b-c"]` and `a.b-c` name the same key: the brackets and the quotes are
|
|
22
|
+
* notation, not part of the name.
|
|
23
|
+
*/
|
|
24
|
+
export function parseRedactPath(path: string): string[] {
|
|
25
|
+
const tokens = path.match(PATH_TOKEN);
|
|
26
|
+
if (!tokens) return [];
|
|
27
|
+
return tokens.map((token) => {
|
|
28
|
+
const inner = token.startsWith("[") ? token.slice(1, -1) : token;
|
|
29
|
+
return inner.replace(/['"`]/g, "");
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Replace everything `tokens` selects with `censor`, without touching the
|
|
35
|
+
* input.
|
|
36
|
+
*
|
|
37
|
+
* Copy-on-write, and the copy is only made once something actually matched —
|
|
38
|
+
* so an entry with no redacted field is passed through by reference, which is
|
|
39
|
+
* the common case on the hot path. The same rule is what keeps the walk
|
|
40
|
+
* honest: a path that names nothing leaves the object exactly as it was
|
|
41
|
+
* instead of inventing the key it was looking for.
|
|
42
|
+
*/
|
|
43
|
+
export function redactPath(
|
|
44
|
+
node: unknown,
|
|
45
|
+
tokens: readonly string[],
|
|
46
|
+
censor: string,
|
|
47
|
+
depth = 0,
|
|
48
|
+
): unknown {
|
|
49
|
+
const segment = tokens[depth];
|
|
50
|
+
if (segment === undefined || node === null || typeof node !== "object") {
|
|
51
|
+
return node;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const keys = selectKeys(node, segment);
|
|
55
|
+
if (keys.length === 0) return node;
|
|
56
|
+
|
|
57
|
+
const isLast = depth === tokens.length - 1;
|
|
58
|
+
let copy: Record<string, unknown> | unknown[] | null = null;
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
const current = readKey(node, key);
|
|
61
|
+
const next = isLast
|
|
62
|
+
? censor
|
|
63
|
+
: redactPath(current, tokens, censor, depth + 1);
|
|
64
|
+
if (next === current) continue;
|
|
65
|
+
if (!copy) copy = Array.isArray(node) ? [...node] : { ...node };
|
|
66
|
+
writeKey(copy, key, next);
|
|
67
|
+
}
|
|
68
|
+
return copy ?? node;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The own keys a path segment selects.
|
|
73
|
+
*
|
|
74
|
+
* `*` takes every one of them — on an array too, whose own keys are its
|
|
75
|
+
* indices, which is what makes `users[*].token` reach into a list. Anything
|
|
76
|
+
* else takes that single key, and only if the object really has it: `in` would
|
|
77
|
+
* answer for `toString` and turn a redaction into an invented field.
|
|
78
|
+
*/
|
|
79
|
+
function selectKeys(node: object, segment: string): string[] {
|
|
80
|
+
if (segment === "*") return Object.keys(node);
|
|
81
|
+
return Object.hasOwn(node, segment) ? [segment] : [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function readKey(node: object, key: string): unknown {
|
|
85
|
+
const own = Object.getOwnPropertyDescriptor(node, key);
|
|
86
|
+
return own === undefined ? undefined : own.value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function writeKey(
|
|
90
|
+
node: Record<string, unknown> | unknown[],
|
|
91
|
+
key: string,
|
|
92
|
+
value: unknown,
|
|
93
|
+
): void {
|
|
94
|
+
if (Array.isArray(node)) {
|
|
95
|
+
const index = Number(key);
|
|
96
|
+
if (Number.isInteger(index)) node[index] = value;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
node[key] = value;
|
|
100
|
+
}
|
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;
|