lograil 0.1.5 → 0.1.6
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/CHANGELOG.md +6 -0
- package/dist/cjs/core/logger.d.ts +23 -14
- package/dist/cjs/core/logger.d.ts.map +1 -1
- package/dist/cjs/core/logger.js +115 -191
- package/dist/cjs/core/logger.js.map +1 -1
- package/dist/cjs/core/transport-write-queue.d.ts +18 -0
- package/dist/cjs/core/transport-write-queue.d.ts.map +1 -0
- package/dist/cjs/core/transport-write-queue.js +86 -0
- package/dist/cjs/core/transport-write-queue.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -12
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +10 -12
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/pipeline/processor.d.ts.map +1 -1
- package/dist/cjs/pipeline/processor.js +27 -12
- package/dist/cjs/pipeline/processor.js.map +1 -1
- package/dist/cjs/plugin/manager.d.ts +4 -1
- package/dist/cjs/plugin/manager.d.ts.map +1 -1
- package/dist/cjs/plugin/manager.js +36 -5
- package/dist/cjs/plugin/manager.js.map +1 -1
- package/dist/cjs/runtime/adapter.d.ts +2 -0
- package/dist/cjs/runtime/adapter.d.ts.map +1 -1
- package/dist/cjs/runtime/electron-main.d.ts.map +1 -1
- package/dist/cjs/runtime/electron-main.js +4 -0
- package/dist/cjs/runtime/electron-main.js.map +1 -1
- package/dist/cjs/runtime/electron-renderer.d.ts.map +1 -1
- package/dist/cjs/runtime/electron-renderer.js +4 -0
- package/dist/cjs/runtime/electron-renderer.js.map +1 -1
- package/dist/cjs/runtime/node.d.ts.map +1 -1
- package/dist/cjs/runtime/node.js +24 -17
- package/dist/cjs/runtime/node.js.map +1 -1
- package/dist/esm/core/logger.d.ts +23 -14
- package/dist/esm/core/logger.d.ts.map +1 -1
- package/dist/esm/core/logger.js +115 -191
- package/dist/esm/core/logger.js.map +1 -1
- package/dist/esm/core/transport-write-queue.d.ts +18 -0
- package/dist/esm/core/transport-write-queue.d.ts.map +1 -0
- package/dist/esm/core/transport-write-queue.js +82 -0
- package/dist/esm/core/transport-write-queue.js.map +1 -0
- package/dist/esm/index.d.ts +10 -12
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +10 -12
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/pipeline/processor.d.ts.map +1 -1
- package/dist/esm/pipeline/processor.js +27 -12
- package/dist/esm/pipeline/processor.js.map +1 -1
- package/dist/esm/plugin/manager.d.ts +4 -1
- package/dist/esm/plugin/manager.d.ts.map +1 -1
- package/dist/esm/plugin/manager.js +36 -5
- package/dist/esm/plugin/manager.js.map +1 -1
- package/dist/esm/runtime/adapter.d.ts +2 -0
- package/dist/esm/runtime/adapter.d.ts.map +1 -1
- package/dist/esm/runtime/electron-main.d.ts.map +1 -1
- package/dist/esm/runtime/electron-main.js +4 -0
- package/dist/esm/runtime/electron-main.js.map +1 -1
- package/dist/esm/runtime/electron-renderer.d.ts.map +1 -1
- package/dist/esm/runtime/electron-renderer.js +4 -0
- package/dist/esm/runtime/electron-renderer.js.map +1 -1
- package/dist/esm/runtime/node.d.ts.map +1 -1
- package/dist/esm/runtime/node.js +24 -17
- package/dist/esm/runtime/node.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,11 @@ All notable changes to this project are documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). This file is generated from git history by `scripts/gen-changelog.mjs` (run via `yarn changelog`) and refreshed automatically on each release.
|
|
6
6
|
|
|
7
|
+
## [v0.1.6] - 2026-10-16
|
|
8
|
+
|
|
9
|
+
- 0.1.6
|
|
10
|
+
- Harden logging dispatch, processing, and runtime boundaries (#9)
|
|
11
|
+
|
|
7
12
|
## [v0.1.5] - 2026-10-13
|
|
8
13
|
|
|
9
14
|
- fix(tsconfig): add DOM lib to test config for browser API tests
|
|
@@ -194,3 +199,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
194
199
|
[v0.1.3]: https://github.com/axetroy/lograil/compare/v0.1.2...v0.1.3
|
|
195
200
|
[v0.1.4]: https://github.com/axetroy/lograil/compare/v0.1.3...v0.1.4
|
|
196
201
|
[v0.1.5]: https://github.com/axetroy/lograil/compare/v0.1.4...v0.1.5
|
|
202
|
+
[v0.1.6]: https://github.com/axetroy/lograil/compare/v0.1.5...v0.1.6
|
|
@@ -76,14 +76,14 @@ export interface LoggerOptions {
|
|
|
76
76
|
/** Shared plugin manager. Internal: used by {@link scope}. */
|
|
77
77
|
plugins?: PluginManager;
|
|
78
78
|
/**
|
|
79
|
-
* Register
|
|
80
|
-
*
|
|
81
|
-
* Only effective in Node; ignored elsewhere. See {@link attachExitHandlers}.
|
|
79
|
+
* Register the runtime's host-shutdown hook that flushes the logger before
|
|
80
|
+
* exit. See {@link attachExitHandlers}.
|
|
82
81
|
*/
|
|
83
82
|
autoFlushOnExit?: boolean;
|
|
84
83
|
/**
|
|
85
84
|
* Environment variable (name) whose value, if set to a valid level name,
|
|
86
|
-
*
|
|
85
|
+
* overrides `level`. Defaults to `"LOG_LEVEL"` when supported by the runtime.
|
|
86
|
+
* Set to `null` to disable.
|
|
87
87
|
*/
|
|
88
88
|
levelEnvVar?: string | null;
|
|
89
89
|
/**
|
|
@@ -93,9 +93,9 @@ export interface LoggerOptions {
|
|
|
93
93
|
*/
|
|
94
94
|
scopeFilter?: string | string[];
|
|
95
95
|
/**
|
|
96
|
-
*
|
|
97
|
-
* `scopeFilter` is not set. Defaults to `"LOGRAIL_DEBUG"
|
|
98
|
-
* to disable.
|
|
96
|
+
* Runtime environment variable whose value supplies the scope filter when
|
|
97
|
+
* `scopeFilter` is not set. Defaults to `"LOGRAIL_DEBUG"` when supported.
|
|
98
|
+
* Set to `null` to disable.
|
|
99
99
|
*/
|
|
100
100
|
scopeFilterEnvVar?: string | null;
|
|
101
101
|
}
|
|
@@ -125,8 +125,11 @@ interface ChildOptions {
|
|
|
125
125
|
export declare class Logger implements LoggerMethods {
|
|
126
126
|
private runtime;
|
|
127
127
|
private pipeline;
|
|
128
|
+
private finalRedactProcessor?;
|
|
128
129
|
private plugins;
|
|
129
130
|
private transports;
|
|
131
|
+
private defaultTransportsPending;
|
|
132
|
+
private runtimeInitialized;
|
|
130
133
|
private context;
|
|
131
134
|
private level;
|
|
132
135
|
private scopeName?;
|
|
@@ -146,16 +149,15 @@ export declare class Logger implements LoggerMethods {
|
|
|
146
149
|
private transportQueues;
|
|
147
150
|
private destroyed;
|
|
148
151
|
private detachReceiver?;
|
|
149
|
-
private
|
|
152
|
+
private lifecycleHandlersAttached;
|
|
150
153
|
private errorHandlersAttached;
|
|
151
|
-
private
|
|
154
|
+
private removeLifecycleHandlers?;
|
|
152
155
|
/** Restores the global `console` methods when `redirectConsole` was called. */
|
|
153
156
|
private restoreConsole?;
|
|
154
157
|
private onLoggerError?;
|
|
155
158
|
private writeTimeoutMs;
|
|
156
159
|
private flushTimeoutMs;
|
|
157
160
|
private maxQueueDepth;
|
|
158
|
-
private _exiting;
|
|
159
161
|
private scopeFilter?;
|
|
160
162
|
/** True for loggers created via `scope`/`child` (share the parent's plumbing). */
|
|
161
163
|
private isChild;
|
|
@@ -173,6 +175,13 @@ export declare class Logger implements LoggerMethods {
|
|
|
173
175
|
* IPC receiver registration) happens.
|
|
174
176
|
*/
|
|
175
177
|
private initFromParent;
|
|
178
|
+
/**
|
|
179
|
+
* Initialize runtime-owned resources. This is called automatically by the
|
|
180
|
+
* first logger operation that needs transports or peer-message reception.
|
|
181
|
+
* Call it explicitly when a process must receive peer logs before emitting
|
|
182
|
+
* any local entries.
|
|
183
|
+
*/
|
|
184
|
+
initialize(): this;
|
|
176
185
|
/**
|
|
177
186
|
* Feed an externally produced {@link LogEntry} (e.g. received from a renderer
|
|
178
187
|
* process over IPC) into the pipeline/transports. Subject to the configured
|
|
@@ -213,6 +222,7 @@ export declare class Logger implements LoggerMethods {
|
|
|
213
222
|
*/
|
|
214
223
|
addTransport(transport: Transport): void;
|
|
215
224
|
removeTransport(name: string): void;
|
|
225
|
+
private getTransportQueue;
|
|
216
226
|
/** Unregister a plugin by name at runtime. */
|
|
217
227
|
unregisterPlugin(name: string): void;
|
|
218
228
|
/** Whether a plugin with the given name is currently registered. */
|
|
@@ -293,15 +303,14 @@ export declare class Logger implements LoggerMethods {
|
|
|
293
303
|
/** Get the current exit-flush timeout (ms). */
|
|
294
304
|
getExitFlushTimeout(): number;
|
|
295
305
|
/**
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* 143) after the flush completes. No-op outside Node. Idempotent.
|
|
306
|
+
* Ask the runtime adapter to flush before host shutdown. The adapter owns
|
|
307
|
+
* event registration and host-specific exit behavior. Idempotent.
|
|
299
308
|
*/
|
|
300
309
|
attachExitHandlers(): void;
|
|
301
310
|
/**
|
|
302
311
|
* Forward `uncaughtException` and `unhandledRejection` to the logger (at
|
|
303
312
|
* `fatal` level) and exit afterwards, so crashes are recorded before the
|
|
304
|
-
* process dies.
|
|
313
|
+
* process dies. Idempotent.
|
|
305
314
|
*/
|
|
306
315
|
watchUncaughtErrors(): void;
|
|
307
316
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/core/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,aAAa,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AAE/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/core/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE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B,2EAA2E;IAC3E,OAAO,CAAC,UAAU;IAclB,0EAA0E;IAC1E,OAAO,CAAC,UAAU;IAOlB,qFAAqF;IACrF,OAAO,CAAC,oBAAoB;IAWtB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB5B;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;IAexB,2EAA2E;IAC3E,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAKrC,+CAA+C;IAC/C,mBAAmB,IAAI,MAAM;IAM7B;;;OAGG;IACH,kBAAkB,IAAI,IAAI;IAgB1B;;;;OAIG;IACH,mBAAmB,IAAI,IAAI;IAiB3B;;;;;;OAMG;IACH,eAAe,IAAI,MAAM,IAAI;IAmCvB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAoC/B"}
|
package/dist/cjs/core/logger.js
CHANGED
|
@@ -10,6 +10,7 @@ const index_js_2 = require("../plugin/index.js");
|
|
|
10
10
|
const index_js_3 = require("../context/index.js");
|
|
11
11
|
const entry_js_1 = require("./entry.js");
|
|
12
12
|
const printf_js_1 = require("./printf.js");
|
|
13
|
+
const transport_write_queue_js_1 = require("./transport-write-queue.js");
|
|
13
14
|
// Captured at module load — before `redirectConsole` can replace `console.*` —
|
|
14
15
|
// so the logger's own error reporting never recurses into itself.
|
|
15
16
|
const RAW_CONSOLE_ERROR = typeof console !== 'undefined' && typeof console.error === 'function'
|
|
@@ -99,23 +100,10 @@ function matchesNamespace(scope, filter) {
|
|
|
99
100
|
return true;
|
|
100
101
|
return filter.includes.some((inc) => inc.re.test(s));
|
|
101
102
|
}
|
|
102
|
-
/** Read an environment variable, returning `undefined` when unavailable/empty. */
|
|
103
|
-
function readEnvVar(name) {
|
|
104
|
-
if (typeof process === 'undefined' || !process.env)
|
|
105
|
-
return undefined;
|
|
106
|
-
const v = process.env[name];
|
|
107
|
-
return typeof v === 'string' && v.length > 0 ? v : undefined;
|
|
108
|
-
}
|
|
109
103
|
/** Narrowing guard for {@link ChildOptions}. */
|
|
110
104
|
function isChildOptions(o) {
|
|
111
105
|
return o.__child === true;
|
|
112
106
|
}
|
|
113
|
-
function getNodeProcess() {
|
|
114
|
-
if (typeof process === 'undefined' || typeof process.on !== 'function') {
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
return process;
|
|
118
|
-
}
|
|
119
107
|
/**
|
|
120
108
|
* Unified logging facade. Produces {@link LogEntry} records, runs them through
|
|
121
109
|
* the {@link Pipeline} (filter + processors), lets {@link Plugin}s intercept,
|
|
@@ -127,6 +115,8 @@ function getNodeProcess() {
|
|
|
127
115
|
*/
|
|
128
116
|
class Logger {
|
|
129
117
|
constructor(options = {}) {
|
|
118
|
+
this.defaultTransportsPending = false;
|
|
119
|
+
this.runtimeInitialized = false;
|
|
130
120
|
/**
|
|
131
121
|
* Bounded async queue for plugin interception. Tracks in-flight entries so
|
|
132
122
|
* `flush()` and `destroy()` know when all interceptions are done without
|
|
@@ -140,10 +130,9 @@ class Logger {
|
|
|
140
130
|
* so overflow can be detected without traversing the promise chain. */
|
|
141
131
|
this.transportQueues = new Map();
|
|
142
132
|
this.destroyed = false;
|
|
143
|
-
this.
|
|
133
|
+
this.lifecycleHandlersAttached = false;
|
|
144
134
|
this.errorHandlersAttached = false;
|
|
145
135
|
this.maxQueueDepth = 0;
|
|
146
|
-
this._exiting = false;
|
|
147
136
|
/** True for loggers created via `scope`/`child` (share the parent's plumbing). */
|
|
148
137
|
this.isChild = false;
|
|
149
138
|
/** Tracks whether the local level was explicitly set via setLevel(). */
|
|
@@ -174,12 +163,16 @@ class Logger {
|
|
|
174
163
|
this.pipeline.setFormatter((0, formatter_js_1.createLineFormatter)());
|
|
175
164
|
}
|
|
176
165
|
if (options.secure !== false) {
|
|
177
|
-
this.
|
|
166
|
+
this.finalRedactProcessor = (0, processor_js_1.createRedactProcessor)();
|
|
167
|
+
this.pipeline.addProcessor(this.finalRedactProcessor);
|
|
178
168
|
}
|
|
179
169
|
this.plugins = options.plugins ?? new index_js_2.PluginManager(this.buildPluginContext());
|
|
180
|
-
this.transports = options.transports ??
|
|
170
|
+
this.transports = options.transports ?? [];
|
|
171
|
+
this.defaultTransportsPending = options.transports === undefined;
|
|
181
172
|
this.context = options.context ?? (0, index_js_3.createContextStore)();
|
|
182
|
-
const envLevelName = options.levelEnvVar === null
|
|
173
|
+
const envLevelName = options.levelEnvVar === null
|
|
174
|
+
? undefined
|
|
175
|
+
: this.runtime.getEnv?.(options.levelEnvVar ?? 'LOG_LEVEL');
|
|
183
176
|
const levelInput = envLevelName && (0, types_js_1.isLogLevelName)(envLevelName) ? envLevelName : (options.level ?? 'info');
|
|
184
177
|
this.level = (0, types_js_1.normalizeLevel)(levelInput);
|
|
185
178
|
this.originalLevel = this.level;
|
|
@@ -187,19 +180,8 @@ class Logger {
|
|
|
187
180
|
const nsInput = options.scopeFilter ??
|
|
188
181
|
(options.scopeFilterEnvVar === null
|
|
189
182
|
? undefined
|
|
190
|
-
:
|
|
183
|
+
: this.runtime.getEnv?.(options.scopeFilterEnvVar ?? 'LOGRAIL_DEBUG'));
|
|
191
184
|
this.scopeFilter = compileNamespaceFilter(nsInput);
|
|
192
|
-
// On the Electron main process / Node primary / Web main thread, receive
|
|
193
|
-
// peer logs over IPC. Level commands sent by peers update `peerLevel`.
|
|
194
|
-
if (this.runtime.attachReceiver) {
|
|
195
|
-
const onPeerLevelCommand = (level) => {
|
|
196
|
-
this.peerLevel = level;
|
|
197
|
-
};
|
|
198
|
-
this.detachReceiver = this.runtime.attachReceiver((entry) => this.ingestEntry(entry), onPeerLevelCommand);
|
|
199
|
-
this.runtime.onLevelCommand = onPeerLevelCommand;
|
|
200
|
-
}
|
|
201
|
-
if (options.autoFlushOnExit)
|
|
202
|
-
this.attachExitHandlers();
|
|
203
185
|
this.onLoggerError = options.onError;
|
|
204
186
|
this.writeTimeoutMs = options.writeTimeoutMs ?? 5000;
|
|
205
187
|
this.flushTimeoutMs = options.flushTimeoutMs ?? 2000;
|
|
@@ -207,6 +189,8 @@ class Logger {
|
|
|
207
189
|
// Wire the pipeline/plugin error sinks into the unified handler.
|
|
208
190
|
this.pipeline.onError = (err, info) => this.reportError(info.phase, err, info.entry);
|
|
209
191
|
this.plugins.onError = (name, err, entry) => this.reportError('plugin', err, entry, name);
|
|
192
|
+
if (options.autoFlushOnExit)
|
|
193
|
+
this.attachExitHandlers();
|
|
210
194
|
}
|
|
211
195
|
/**
|
|
212
196
|
* Initialise a lightweight child from its parent. Reuses the parent's
|
|
@@ -221,6 +205,7 @@ class Logger {
|
|
|
221
205
|
this.pipeline = parent.pipeline;
|
|
222
206
|
this.plugins = parent.plugins;
|
|
223
207
|
this.transports = parent.transports;
|
|
208
|
+
this.finalRedactProcessor = parent.finalRedactProcessor;
|
|
224
209
|
this.scopeFilter = parent.scopeFilter;
|
|
225
210
|
this.context = opts.context ?? (0, index_js_3.createContextStore)();
|
|
226
211
|
this.scopeName = opts.scope;
|
|
@@ -232,6 +217,42 @@ class Logger {
|
|
|
232
217
|
if (opts.levelOverride !== undefined)
|
|
233
218
|
this.levelOverride = opts.levelOverride;
|
|
234
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Initialize runtime-owned resources. This is called automatically by the
|
|
222
|
+
* first logger operation that needs transports or peer-message reception.
|
|
223
|
+
* Call it explicitly when a process must receive peer logs before emitting
|
|
224
|
+
* any local entries.
|
|
225
|
+
*/
|
|
226
|
+
initialize() {
|
|
227
|
+
if (this.destroyed)
|
|
228
|
+
return this;
|
|
229
|
+
if (this.parent) {
|
|
230
|
+
this.parent.initialize();
|
|
231
|
+
this.transports = this.parent.transports;
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
if (this.runtimeInitialized)
|
|
235
|
+
return this;
|
|
236
|
+
this.runtimeInitialized = true;
|
|
237
|
+
try {
|
|
238
|
+
if (this.defaultTransportsPending) {
|
|
239
|
+
this.transports = this.runtime.defaultTransports();
|
|
240
|
+
this.defaultTransportsPending = false;
|
|
241
|
+
}
|
|
242
|
+
if (this.runtime.attachReceiver) {
|
|
243
|
+
const onPeerLevelCommand = (level) => {
|
|
244
|
+
this.peerLevel = level;
|
|
245
|
+
};
|
|
246
|
+
this.detachReceiver = this.runtime.attachReceiver((entry) => this.ingestEntry(entry), onPeerLevelCommand);
|
|
247
|
+
this.runtime.onLevelCommand = onPeerLevelCommand;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
this.runtimeInitialized = false;
|
|
252
|
+
throw err;
|
|
253
|
+
}
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
235
256
|
/**
|
|
236
257
|
* Feed an externally produced {@link LogEntry} (e.g. received from a renderer
|
|
237
258
|
* process over IPC) into the pipeline/transports. Subject to the configured
|
|
@@ -244,7 +265,11 @@ class Logger {
|
|
|
244
265
|
return;
|
|
245
266
|
if (!matchesNamespace(entry.scope, this.scopeFilter))
|
|
246
267
|
return;
|
|
247
|
-
this.
|
|
268
|
+
this.initialize();
|
|
269
|
+
const processed = this.pipeline.process(entry);
|
|
270
|
+
if (!processed)
|
|
271
|
+
return;
|
|
272
|
+
this.dispatch(processed);
|
|
248
273
|
}
|
|
249
274
|
// ---- Level ----
|
|
250
275
|
getLevel() {
|
|
@@ -286,6 +311,7 @@ class Logger {
|
|
|
286
311
|
broadcastLevelToPeers() {
|
|
287
312
|
if (this.parent)
|
|
288
313
|
return; // only the root logger owns the IPC transports
|
|
314
|
+
this.initialize();
|
|
289
315
|
const v = this.getLevel();
|
|
290
316
|
for (const t of this.transports) {
|
|
291
317
|
t.sendLevelCommand?.(v);
|
|
@@ -316,34 +342,37 @@ class Logger {
|
|
|
316
342
|
* existing in-flight queue so queued writes drain first.
|
|
317
343
|
*/
|
|
318
344
|
addTransport(transport) {
|
|
345
|
+
this.initialize();
|
|
319
346
|
const existing = this.transports.filter((t) => t.name === transport.name);
|
|
320
347
|
if (existing.length > 0) {
|
|
321
348
|
this.transports = this.transports.filter((t) => t.name !== transport.name);
|
|
322
349
|
for (const old of existing) {
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
.then(async () => {
|
|
350
|
+
const queue = this.getTransportQueue(old);
|
|
351
|
+
void queue
|
|
352
|
+
.append(async () => {
|
|
327
353
|
if (old.flush)
|
|
328
354
|
await this.guardFlush(old.flush.bind(old));
|
|
329
355
|
if (old.close)
|
|
330
356
|
await Promise.resolve(old.close());
|
|
331
357
|
})
|
|
332
|
-
.catch((err) => this.reportError('transport', err, undefined, old.name))
|
|
333
|
-
|
|
358
|
+
.catch((err) => this.reportError('transport', err, undefined, old.name))
|
|
359
|
+
.finally(() => {
|
|
360
|
+
if (this.transportQueues.get(old) === queue)
|
|
361
|
+
this.transportQueues.delete(old);
|
|
362
|
+
});
|
|
334
363
|
}
|
|
335
364
|
}
|
|
336
365
|
this.transports.push(transport);
|
|
337
366
|
this.plugins.notifyTransport(transport);
|
|
338
367
|
}
|
|
339
368
|
removeTransport(name) {
|
|
369
|
+
this.initialize();
|
|
340
370
|
const removed = this.transports.filter((t) => t.name === name);
|
|
341
371
|
this.transports = this.transports.filter((t) => t.name !== name);
|
|
342
372
|
for (const transport of removed) {
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
.then(async () => {
|
|
373
|
+
const queue = this.getTransportQueue(transport);
|
|
374
|
+
void queue
|
|
375
|
+
.append(async () => {
|
|
347
376
|
if (transport.flush)
|
|
348
377
|
await this.guardFlush(transport.flush.bind(transport));
|
|
349
378
|
if (transport.close)
|
|
@@ -351,11 +380,19 @@ class Logger {
|
|
|
351
380
|
})
|
|
352
381
|
.catch((err) => this.reportError('transport', err, undefined, transport.name))
|
|
353
382
|
.finally(() => {
|
|
354
|
-
this.transportQueues.
|
|
383
|
+
if (this.transportQueues.get(transport) === queue)
|
|
384
|
+
this.transportQueues.delete(transport);
|
|
355
385
|
});
|
|
356
|
-
this.transportQueues.set(transport, { promise: teardown, depth: existing?.depth ?? 0 });
|
|
357
386
|
}
|
|
358
387
|
}
|
|
388
|
+
getTransportQueue(transport) {
|
|
389
|
+
let queue = this.transportQueues.get(transport);
|
|
390
|
+
if (!queue) {
|
|
391
|
+
queue = new transport_write_queue_js_1.TransportWriteQueue(this.writeTimeoutMs, (err, entry, handler) => this.reportTransportError(err, entry, handler));
|
|
392
|
+
this.transportQueues.set(transport, queue);
|
|
393
|
+
}
|
|
394
|
+
return queue;
|
|
395
|
+
}
|
|
359
396
|
/** Unregister a plugin by name at runtime. */
|
|
360
397
|
unregisterPlugin(name) {
|
|
361
398
|
this.plugins.unregister(name);
|
|
@@ -380,6 +417,7 @@ class Logger {
|
|
|
380
417
|
};
|
|
381
418
|
}
|
|
382
419
|
getTransports() {
|
|
420
|
+
this.initialize();
|
|
383
421
|
return this.transports;
|
|
384
422
|
}
|
|
385
423
|
// ---- Plugins ----
|
|
@@ -444,6 +482,7 @@ class Logger {
|
|
|
444
482
|
return;
|
|
445
483
|
if (!matchesNamespace(this.scopeName, this.scopeFilter))
|
|
446
484
|
return;
|
|
485
|
+
this.initialize();
|
|
447
486
|
const entry = this.buildEntry(levelName, levelValue, message, args);
|
|
448
487
|
const processed = this.pipeline.process(entry);
|
|
449
488
|
if (!processed)
|
|
@@ -566,6 +605,15 @@ class Logger {
|
|
|
566
605
|
};
|
|
567
606
|
}
|
|
568
607
|
writeToTransports(entry) {
|
|
608
|
+
if (this.finalRedactProcessor) {
|
|
609
|
+
try {
|
|
610
|
+
entry = this.finalRedactProcessor(entry);
|
|
611
|
+
}
|
|
612
|
+
catch (err) {
|
|
613
|
+
this.reportError('process', err, entry);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
569
617
|
// Freeze at the single fan-out choke point so every transport, formatter and
|
|
570
618
|
// plugin reads an immutable entry and can safely share it by reference
|
|
571
619
|
// (zero-copy within the process). Idempotent.
|
|
@@ -589,56 +637,9 @@ class Logger {
|
|
|
589
637
|
// dropped entry never triggers its side effects (file writes, HTTP
|
|
590
638
|
// requests, IPC sends). The depth check runs first; only if the queue
|
|
591
639
|
// is not full do we call `write()`.
|
|
592
|
-
const existing = this.transportQueues.get(transport);
|
|
593
|
-
const entryDepth = existing?.depth ?? 0;
|
|
594
640
|
// Per-transport limit takes precedence over the global one when both set.
|
|
595
641
|
const limit = transport.queueLimit ?? this.maxQueueDepth;
|
|
596
|
-
|
|
597
|
-
// 1 on accept, +1 for each subsequent write chained onto the same
|
|
598
|
-
// queue, 0 once the chain settles. The overflow check compares the
|
|
599
|
-
// prospective depth (entryDepth + 1) against the limit, so a limit
|
|
600
|
-
// of N admits up to N in-flight writes and drops the (N+1)th.
|
|
601
|
-
if (limit > 0 && entryDepth + 1 > limit) {
|
|
602
|
-
// Drop the newest entry to protect against memory growth.
|
|
603
|
-
if (transport.onOverflow)
|
|
604
|
-
transport.onOverflow(frozen, entryDepth + 1);
|
|
605
|
-
this.reportError('transport', new Error(`queue full (depth ${entryDepth + 1} > ${limit})`), frozen, transport.name);
|
|
606
|
-
continue;
|
|
607
|
-
}
|
|
608
|
-
const result = transport.write(frozen, String(formatted));
|
|
609
|
-
if (result && typeof result.then === 'function') {
|
|
610
|
-
const guarded = this.guardWrite(result);
|
|
611
|
-
// Chain onto THIS transport's own queue, not a shared one, so a stalled
|
|
612
|
-
// write here cannot block other transports (or the front dispatch queue).
|
|
613
|
-
const next = existing?.promise ?? Promise.resolve();
|
|
614
|
-
const chain = next.then(() => guarded);
|
|
615
|
-
// In-flight count: 1 if this is the first write on the queue, else
|
|
616
|
-
// existing.depth + 1 for a write chained onto a queue that already
|
|
617
|
-
// has pending writes. A final settle handler (registered below)
|
|
618
|
-
// resets depth to 0 so the overflow cap re-opens.
|
|
619
|
-
const nextDepth = existing ? existing.depth + 1 : 1;
|
|
620
|
-
const nextEntry = {
|
|
621
|
-
promise: chain.catch((err) => this.reportTransportError(err, frozen, onErr)),
|
|
622
|
-
depth: nextDepth,
|
|
623
|
-
};
|
|
624
|
-
this.transportQueues.set(transport, nextEntry);
|
|
625
|
-
// Reset depth when the queued write settles so the overflow cap
|
|
626
|
-
// re-opens. Chained writes after this point see the up-to-date
|
|
627
|
-
// depth (>=1 while this write is in flight, 0 once it settles).
|
|
628
|
-
chain
|
|
629
|
-
.then(() => {
|
|
630
|
-
const cur = this.transportQueues.get(transport);
|
|
631
|
-
if (cur === nextEntry)
|
|
632
|
-
this.transportQueues.set(transport, { ...cur, depth: 0 });
|
|
633
|
-
})
|
|
634
|
-
.catch(() => {
|
|
635
|
-
// already handled above; still clear depth so a stalled write
|
|
636
|
-
// does not wedge the queue.
|
|
637
|
-
const cur = this.transportQueues.get(transport);
|
|
638
|
-
if (cur === nextEntry)
|
|
639
|
-
this.transportQueues.set(transport, { ...cur, depth: 0 });
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
+
this.getTransportQueue(transport).write(transport, frozen, String(formatted), limit, onErr);
|
|
642
643
|
}
|
|
643
644
|
catch (err) {
|
|
644
645
|
this.reportTransportError(err, frozen, onErr);
|
|
@@ -676,6 +677,7 @@ class Logger {
|
|
|
676
677
|
}
|
|
677
678
|
// ---- Lifecycle ----
|
|
678
679
|
async flush() {
|
|
680
|
+
this.initialize();
|
|
679
681
|
// Let in-flight interception finish so every scheduled write has been
|
|
680
682
|
// enqueued onto its transport's own queue. A new intercepted dispatch can
|
|
681
683
|
// chain onto the same drain promise while we are awaiting it (e.g. an
|
|
@@ -689,7 +691,7 @@ class Logger {
|
|
|
689
691
|
// Aggregate all per-transport async-write queues. `allSettled` means a
|
|
690
692
|
// rejected/errored transport queue never rejects flush; a stalled one is
|
|
691
693
|
// bounded by `writeTimeoutMs` via `guardWrite`.
|
|
692
|
-
await Promise.allSettled(Array.from(this.transportQueues.values()).map((
|
|
694
|
+
await Promise.allSettled(Array.from(this.transportQueues.values()).map((queue) => queue.drain()));
|
|
693
695
|
// Flush each transport's internal buffers with the same timeout guard so
|
|
694
696
|
// a stalled flush can never hang shutdown or caller code.
|
|
695
697
|
await Promise.allSettled(this.transports.map((t) => (t.flush ? this.guardFlush(t.flush.bind(t)) : Promise.resolve())));
|
|
@@ -726,127 +728,49 @@ class Logger {
|
|
|
726
728
|
getExitFlushTimeout() {
|
|
727
729
|
return this.flushTimeoutMs;
|
|
728
730
|
}
|
|
729
|
-
// ----
|
|
731
|
+
// ---- Runtime lifecycle integration ----
|
|
730
732
|
/**
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
* 143) after the flush completes. No-op outside Node. Idempotent.
|
|
733
|
+
* Ask the runtime adapter to flush before host shutdown. The adapter owns
|
|
734
|
+
* event registration and host-specific exit behavior. Idempotent.
|
|
734
735
|
*/
|
|
735
736
|
attachExitHandlers() {
|
|
736
|
-
if (this.
|
|
737
|
+
if (this.lifecycleHandlersAttached)
|
|
737
738
|
return;
|
|
738
|
-
// Prefer runtime-provided lifecycle hooks so the logger stays
|
|
739
|
-
// runtime-agnostic and the runtime owns process/window event wiring and
|
|
740
|
-
// exit semantics. Falls back to a direct `process` binding below for
|
|
741
|
-
// adapters that don't supply lifecycle hooks (e.g. custom test runtimes).
|
|
742
739
|
const lc = this.runtime.lifecycle;
|
|
743
|
-
if (lc)
|
|
744
|
-
const ms = this.flushTimeoutMs;
|
|
745
|
-
const detach = lc.onFlushBeforeExit(() => {
|
|
746
|
-
void this.flushWithTimeout(ms);
|
|
747
|
-
});
|
|
748
|
-
const prev = this.removeProcessHandlers;
|
|
749
|
-
this.removeProcessHandlers = () => {
|
|
750
|
-
prev?.();
|
|
751
|
-
detach();
|
|
752
|
-
};
|
|
753
|
-
this.processHandlersAttached = true;
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
const proc = getNodeProcess();
|
|
757
|
-
if (!proc)
|
|
740
|
+
if (!lc)
|
|
758
741
|
return;
|
|
759
742
|
const ms = this.flushTimeoutMs;
|
|
760
|
-
|
|
761
|
-
// `beforeExit` re-fires as long as async writes keep the loop alive, so the
|
|
762
|
-
// queue keeps draining until empty (bounded by `ms` if a sink stalls).
|
|
763
|
-
const onBeforeExit = () => {
|
|
743
|
+
const detach = lc.onFlushBeforeExit(() => {
|
|
764
744
|
void this.flushWithTimeout(ms);
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
if (this._exiting)
|
|
770
|
-
return;
|
|
771
|
-
this._exiting = true;
|
|
772
|
-
void this.flushWithTimeout(ms).finally(() => {
|
|
773
|
-
try {
|
|
774
|
-
proc.exit(code);
|
|
775
|
-
}
|
|
776
|
-
catch {
|
|
777
|
-
/* ignore */
|
|
778
|
-
}
|
|
779
|
-
});
|
|
780
|
-
};
|
|
781
|
-
const onSigInt = () => onSignal(130);
|
|
782
|
-
const onSigTerm = () => onSignal(143);
|
|
783
|
-
proc.on('beforeExit', onBeforeExit);
|
|
784
|
-
proc.on('SIGINT', onSigInt);
|
|
785
|
-
proc.on('SIGTERM', onSigTerm);
|
|
786
|
-
// Windows does not emit `SIGTERM`. Register `SIGBREAK` (Ctrl+Break) as a
|
|
787
|
-
// best-effort fallback so `autoFlushOnExit` has a signal hook on that
|
|
788
|
-
// platform. In practice taskkill / OS shutdown kills without emitting any
|
|
789
|
-
// Node signal, so no handler can cover that path — the `beforeExit` hook
|
|
790
|
-
// is the only portable shutdown flush mechanism.
|
|
791
|
-
if (process.platform === 'win32') {
|
|
792
|
-
proc.on('SIGBREAK', onSigInt);
|
|
793
|
-
}
|
|
794
|
-
this.processHandlersAttached = true;
|
|
795
|
-
const prev = this.removeProcessHandlers;
|
|
796
|
-
this.removeProcessHandlers = () => {
|
|
745
|
+
});
|
|
746
|
+
this.lifecycleHandlersAttached = true;
|
|
747
|
+
const prev = this.removeLifecycleHandlers;
|
|
748
|
+
this.removeLifecycleHandlers = () => {
|
|
797
749
|
prev?.();
|
|
798
|
-
|
|
799
|
-
proc.removeListener('SIGINT', onSigInt);
|
|
800
|
-
proc.removeListener('SIGTERM', onSigTerm);
|
|
750
|
+
detach();
|
|
801
751
|
};
|
|
802
752
|
}
|
|
803
753
|
/**
|
|
804
754
|
* Forward `uncaughtException` and `unhandledRejection` to the logger (at
|
|
805
755
|
* `fatal` level) and exit afterwards, so crashes are recorded before the
|
|
806
|
-
* process dies.
|
|
756
|
+
* process dies. Idempotent.
|
|
807
757
|
*/
|
|
808
758
|
watchUncaughtErrors() {
|
|
809
759
|
if (this.errorHandlersAttached)
|
|
810
760
|
return;
|
|
811
|
-
// Delegate to the runtime's lifecycle hooks when present (see
|
|
812
|
-
// `attachExitHandlers` for the rationale). The hooks own the `process`
|
|
813
|
-
// events and the post-flush `exit(1)`; we only provide the fatal log +
|
|
814
|
-
// bounded flush. Runtimes without an `onUncaughtError` hook (e.g. web)
|
|
815
|
-
// fall back to the direct `process` binding below, which is a no-op there.
|
|
816
761
|
const lc = this.runtime.lifecycle;
|
|
817
|
-
if (lc?.onUncaughtError)
|
|
818
|
-
const ms = this.flushTimeoutMs;
|
|
819
|
-
const detach = lc.onUncaughtError((err) => {
|
|
820
|
-
this.fatal(err);
|
|
821
|
-
return this.flushWithTimeout(ms);
|
|
822
|
-
});
|
|
823
|
-
const prev = this.removeProcessHandlers;
|
|
824
|
-
this.removeProcessHandlers = () => {
|
|
825
|
-
prev?.();
|
|
826
|
-
detach();
|
|
827
|
-
};
|
|
828
|
-
this.errorHandlersAttached = true;
|
|
829
|
-
return;
|
|
830
|
-
}
|
|
831
|
-
const proc = getNodeProcess();
|
|
832
|
-
if (!proc)
|
|
762
|
+
if (!lc?.onUncaughtError)
|
|
833
763
|
return;
|
|
834
|
-
const
|
|
764
|
+
const ms = this.flushTimeoutMs;
|
|
765
|
+
const detach = lc.onUncaughtError((err) => {
|
|
835
766
|
this.fatal(err);
|
|
836
|
-
|
|
837
|
-
};
|
|
838
|
-
const onRejection = (reason) => {
|
|
839
|
-
this.fatal(reason);
|
|
840
|
-
void this.flushWithTimeout(this.flushTimeoutMs).finally(() => proc.exit(1));
|
|
841
|
-
};
|
|
842
|
-
proc.on('uncaughtException', onUncaught);
|
|
843
|
-
proc.on('unhandledRejection', onRejection);
|
|
767
|
+
return this.flushWithTimeout(ms);
|
|
768
|
+
});
|
|
844
769
|
this.errorHandlersAttached = true;
|
|
845
|
-
const prev = this.
|
|
846
|
-
this.
|
|
770
|
+
const prev = this.removeLifecycleHandlers;
|
|
771
|
+
this.removeLifecycleHandlers = () => {
|
|
847
772
|
prev?.();
|
|
848
|
-
|
|
849
|
-
proc.removeListener('unhandledRejection', onRejection);
|
|
773
|
+
detach();
|
|
850
774
|
};
|
|
851
775
|
}
|
|
852
776
|
/**
|
|
@@ -902,7 +826,7 @@ class Logger {
|
|
|
902
826
|
this.transportQueues.clear();
|
|
903
827
|
return;
|
|
904
828
|
}
|
|
905
|
-
this.
|
|
829
|
+
this.removeLifecycleHandlers?.();
|
|
906
830
|
// Restore the global `console` if `redirectConsole` was called but its
|
|
907
831
|
// returned restore function was never invoked — otherwise the replacement
|
|
908
832
|
// would outlive the logger.
|