@hamak/logging 0.12.0 → 0.12.2
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/dist/api/interfaces/transport.d.ts +6 -1
- package/dist/api/interfaces/transport.d.ts.map +1 -1
- package/dist/impl/core/log-manager.d.ts.map +1 -1
- package/dist/impl/core/log-manager.js +6 -5
- package/dist/impl/transports/console-transport.d.ts +4 -0
- package/dist/impl/transports/console-transport.d.ts.map +1 -1
- package/dist/impl/transports/console-transport.js +12 -7
- package/dist/impl/utils/console-interceptor.d.ts +46 -11
- package/dist/impl/utils/console-interceptor.d.ts.map +1 -1
- package/dist/impl/utils/console-interceptor.js +128 -47
- package/dist/impl/utils/event-emitter.d.ts.map +1 -1
- package/dist/impl/utils/event-emitter.js +2 -1
- package/dist/impl/utils/index.d.ts +1 -0
- package/dist/impl/utils/index.d.ts.map +1 -1
- package/dist/impl/utils/index.js +1 -0
- package/dist/impl/utils/safe-console.d.ts +10 -0
- package/dist/impl/utils/safe-console.d.ts.map +1 -0
- package/dist/impl/utils/safe-console.js +34 -0
- package/package.json +6 -4
|
@@ -21,6 +21,11 @@ import { LogEntry } from '../types/index.js';
|
|
|
21
21
|
* }
|
|
22
22
|
* }
|
|
23
23
|
* ```
|
|
24
|
+
*
|
|
25
|
+
* A transport must write through `safeConsole` (`@hamak/logging`), never the
|
|
26
|
+
* global `console`. With console interception enabled — the plugin's default —
|
|
27
|
+
* the globals log, so a transport writing to them feeds every record it emits
|
|
28
|
+
* straight back into the logger, unbounded (amah/app-framework#85).
|
|
24
29
|
*/
|
|
25
30
|
export interface ILogTransport {
|
|
26
31
|
/** Unique identifier for the transport */
|
|
@@ -63,7 +68,7 @@ export interface ILogTransport {
|
|
|
63
68
|
* @example
|
|
64
69
|
* ```typescript
|
|
65
70
|
* onError(error: Error): void {
|
|
66
|
-
*
|
|
71
|
+
* safeConsole.error('[Transport Error]', error);
|
|
67
72
|
* // Maybe send to error tracking service
|
|
68
73
|
* }
|
|
69
74
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../src/api/interfaces/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../../src/api/interfaces/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,WAAW,aAAa;IAC5B,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB,0BAA0B;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3C;;;OAGG;IACH,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;OAGG;IACH,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAExB;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC;IAErC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CAC9B;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;;;;;;OASG;IACH,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,GAAG,GAAG,CAAC;IAEtC;;;OAGG;IACH,WAAW,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,GAAG,MAAM,GAAG,GAAG,CAAC;CACjD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log-manager.d.ts","sourceRoot":"","sources":["../../../src/impl/core/log-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,OAAO,EACP,aAAa,EACb,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,gBAAgB,EAChB,eAAe,EAChB,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"log-manager.d.ts","sourceRoot":"","sources":["../../../src/impl/core/log-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,OAAO,EACP,aAAa,EACb,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,gBAAgB,EAChB,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAKzB;;;;GAIG;AACH,qBAAa,UAAW,YAAW,WAAW;IAC5C,OAAO,CAAC,UAAU,CAAoC;IACtD,OAAO,CAAC,gBAAgB,CAAsC;IAC9D,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,UAAU,CAAC,CAAiC;gBAExC,MAAM,GAAE,gBAAqB;IAezC,iBAAiB,CAAC,SAAS,EAAE,aAAa,EAAE,MAAM,CAAC,EAAE,eAAe,GAAG,IAAI;IAe3E,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAWrC,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS;IAInD,cAAc,IAAI,aAAa,EAAE;IAIjC,YAAY,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO;IAK3C,SAAS,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO;IAUxC,cAAc,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAIrC,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,GAAG,IAAI;IAIrD,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,GAAG,IAAI;IAI/C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA2B5B,WAAW,IAAI,IAAI;IAInB,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAI7E,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAI3D,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IAoB/B;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;YAoBhB,oBAAoB;IAyBlC,OAAO,CAAC,cAAc;IA8CtB,OAAO,CAAC,YAAY;IAkBpB,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,oBAAoB;CAa7B"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ContextLogger } from './context-logger.js';
|
|
2
2
|
import { EventEmitter } from '../utils/event-emitter.js';
|
|
3
|
+
import { safeConsole } from '../utils/safe-console.js';
|
|
3
4
|
/**
|
|
4
5
|
* Central log manager implementation
|
|
5
6
|
*
|
|
@@ -148,13 +149,13 @@ export class LogManager {
|
|
|
148
149
|
// Immediate dispatch for high-priority logs
|
|
149
150
|
if (entry.level >= 4) { // LogLevel.ERROR
|
|
150
151
|
this.dispatchToTransports(entry).catch(error => {
|
|
151
|
-
|
|
152
|
+
safeConsole.error('[LogManager] Error dispatching log entry:', error);
|
|
152
153
|
});
|
|
153
154
|
}
|
|
154
155
|
// Flush if buffer is full
|
|
155
156
|
if (this.buffer.length >= this.config.bufferSize) {
|
|
156
157
|
this.flush().catch(error => {
|
|
157
|
-
|
|
158
|
+
safeConsole.error('[LogManager] Error flushing buffer:', error);
|
|
158
159
|
});
|
|
159
160
|
}
|
|
160
161
|
}
|
|
@@ -257,7 +258,7 @@ export class LogManager {
|
|
|
257
258
|
startFlushTimer() {
|
|
258
259
|
this.flushTimer = setInterval(() => {
|
|
259
260
|
this.flush().catch(error => {
|
|
260
|
-
|
|
261
|
+
safeConsole.error('[LogManager] Error in flush timer:', error);
|
|
261
262
|
});
|
|
262
263
|
}, this.config.flushInterval);
|
|
263
264
|
}
|
|
@@ -268,11 +269,11 @@ export class LogManager {
|
|
|
268
269
|
transport.onError(error);
|
|
269
270
|
}
|
|
270
271
|
catch (handlerError) {
|
|
271
|
-
|
|
272
|
+
safeConsole.error('[LogManager] Error in transport error handler:', handlerError);
|
|
272
273
|
}
|
|
273
274
|
}
|
|
274
275
|
else {
|
|
275
|
-
|
|
276
|
+
safeConsole.error(`[LogManager] Transport '${transport.id}' error:`, error);
|
|
276
277
|
}
|
|
277
278
|
}
|
|
278
279
|
}
|
|
@@ -4,6 +4,10 @@ import type { ILogTransport, ILogFormatter, LogEntry } from '../../api/index.js'
|
|
|
4
4
|
*
|
|
5
5
|
* Outputs logs to the browser console or Node.js console.
|
|
6
6
|
* Supports formatting via pluggable formatters.
|
|
7
|
+
*
|
|
8
|
+
* Writes through {@link safeConsole}, not the global console: with console
|
|
9
|
+
* interception enabled the globals log, so writing through them would feed
|
|
10
|
+
* every record straight back into the logger (#85).
|
|
7
11
|
*/
|
|
8
12
|
export declare class ConsoleTransport implements ILogTransport {
|
|
9
13
|
private formatter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"console-transport.d.ts","sourceRoot":"","sources":["../../../src/impl/transports/console-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"console-transport.d.ts","sourceRoot":"","sources":["../../../src/impl/transports/console-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAG9E;;;;;;;;;GASG;AACH,qBAAa,gBAAiB,YAAW,aAAa;IAIxC,OAAO,CAAC,SAAS;IAH7B,QAAQ,CAAC,EAAE,aAAa;IACxB,QAAQ,CAAC,IAAI,uBAAuB;gBAEhB,SAAS,EAAE,aAAa;IAE5C,GAAG,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI;IA8B1B,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAG5B"}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { safeConsole } from '../utils/safe-console.js';
|
|
1
2
|
/**
|
|
2
3
|
* Console transport
|
|
3
4
|
*
|
|
4
5
|
* Outputs logs to the browser console or Node.js console.
|
|
5
6
|
* Supports formatting via pluggable formatters.
|
|
7
|
+
*
|
|
8
|
+
* Writes through {@link safeConsole}, not the global console: with console
|
|
9
|
+
* interception enabled the globals log, so writing through them would feed
|
|
10
|
+
* every record straight back into the logger (#85).
|
|
6
11
|
*/
|
|
7
12
|
export class ConsoleTransport {
|
|
8
13
|
constructor(formatter) {
|
|
@@ -30,26 +35,26 @@ export class ConsoleTransport {
|
|
|
30
35
|
switch (entry.level) {
|
|
31
36
|
case 0: // TRACE
|
|
32
37
|
case 1: // DEBUG
|
|
33
|
-
|
|
38
|
+
safeConsole.debug(formatted);
|
|
34
39
|
break;
|
|
35
40
|
case 2: // INFO
|
|
36
|
-
|
|
41
|
+
safeConsole.info(formatted);
|
|
37
42
|
break;
|
|
38
43
|
case 3: // WARN
|
|
39
|
-
|
|
44
|
+
safeConsole.warn(formatted);
|
|
40
45
|
break;
|
|
41
46
|
case 4: // ERROR
|
|
42
47
|
case 5: // FATAL
|
|
43
|
-
|
|
48
|
+
safeConsole.error(formatted);
|
|
44
49
|
if (entry.metadata?.error) {
|
|
45
|
-
|
|
50
|
+
safeConsole.error(entry.metadata.error);
|
|
46
51
|
}
|
|
47
52
|
break;
|
|
48
53
|
default:
|
|
49
|
-
|
|
54
|
+
safeConsole.log(formatted);
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
onError(error) {
|
|
53
|
-
|
|
58
|
+
safeConsole.error('[ConsoleTransport] Error:', error);
|
|
54
59
|
}
|
|
55
60
|
}
|
|
@@ -1,33 +1,68 @@
|
|
|
1
1
|
import type { ILogManager } from '../../api/index.js';
|
|
2
|
+
import { type SafeConsole } from './safe-console.js';
|
|
2
3
|
/**
|
|
3
4
|
* Console interceptor that routes console methods through the logging system
|
|
4
5
|
*
|
|
5
6
|
* By default, this is enabled. Set `interceptConsole: false` in LoggingPluginConfig to disable.
|
|
7
|
+
*
|
|
8
|
+
* The traffic is one-way: application `console.*` calls reach the logger, and
|
|
9
|
+
* the logger's own output goes to {@link safeConsole}, the copy captured
|
|
10
|
+
* before any interception. The console transport writing through the patched
|
|
11
|
+
* globals is what made the default configuration exhaust the heap (#85).
|
|
6
12
|
*/
|
|
7
13
|
export declare class ConsoleInterceptor {
|
|
8
14
|
private logManager;
|
|
9
|
-
|
|
15
|
+
/**
|
|
16
|
+
* This interceptor's live patch, while it is intercepting.
|
|
17
|
+
*
|
|
18
|
+
* `previous` is the globals as they were the moment `intercept()` patched
|
|
19
|
+
* them, so restoring is an exact undo rather than a reset to
|
|
20
|
+
* {@link safeConsole}: a host that wrapped `console` after importing this
|
|
21
|
+
* package (a breadcrumb hook, a test harness) keeps its wrapper.
|
|
22
|
+
*
|
|
23
|
+
* What the logging system WRITES through stays {@link safeConsole}, the
|
|
24
|
+
* module-load copy: an `intercept()`-time snapshot could capture another
|
|
25
|
+
* interceptor's patched methods and chain two loggers together (#85).
|
|
26
|
+
*/
|
|
27
|
+
private record?;
|
|
10
28
|
private isIntercepting;
|
|
11
29
|
constructor(logManager: ILogManager);
|
|
30
|
+
/**
|
|
31
|
+
* The globals as they are right now, to be put back verbatim later.
|
|
32
|
+
*
|
|
33
|
+
* Captured WITHOUT binding: `restore()` reinstalls these as console methods,
|
|
34
|
+
* where `this` is the console again, and a caller comparing
|
|
35
|
+
* `console.log === theirs` after a restore expects the very function it
|
|
36
|
+
* installed — a bound copy would not be it. (The module-load
|
|
37
|
+
* {@link safeConsole} does bind, because it is called detached.)
|
|
38
|
+
*/
|
|
39
|
+
private snapshotConsole;
|
|
12
40
|
/**
|
|
13
41
|
* Start intercepting console methods
|
|
14
42
|
*/
|
|
15
43
|
intercept(): void;
|
|
16
44
|
/**
|
|
17
|
-
* Stop intercepting and restore original console methods
|
|
45
|
+
* Stop intercepting and restore original console methods.
|
|
46
|
+
*
|
|
47
|
+
* Interceptors nest — the plugin intercepts in `activate()` and restores in
|
|
48
|
+
* `deactivate()`, so two hosts can be active at once and may shut down in
|
|
49
|
+
* any order. Restoring by plain assignment is only correct for whoever is on
|
|
50
|
+
* top; an earlier interceptor doing it would reinstall a patch belonging to
|
|
51
|
+
* a logger that is being shut down, and every later `console.log` would
|
|
52
|
+
* disappear into it.
|
|
53
|
+
*
|
|
54
|
+
* So a restore UNLINKS instead: if we are on top, put our snapshot back;
|
|
55
|
+
* otherwise hand our snapshot to whoever patched over us, so their own
|
|
56
|
+
* restore reinstalls it. If neither holds — something outside this package
|
|
57
|
+
* owns the global now — we leave it alone rather than clobber it.
|
|
18
58
|
*/
|
|
19
59
|
restore(): void;
|
|
20
60
|
/**
|
|
21
|
-
*
|
|
61
|
+
* The console the logging system itself writes through: the copy captured at
|
|
62
|
+
* module load, bound to `console`, so callers may hold it detached. Never
|
|
63
|
+
* the patched globals.
|
|
22
64
|
*/
|
|
23
|
-
getOriginalConsole():
|
|
24
|
-
log: typeof console.log;
|
|
25
|
-
debug: typeof console.debug;
|
|
26
|
-
info: typeof console.info;
|
|
27
|
-
warn: typeof console.warn;
|
|
28
|
-
error: typeof console.error;
|
|
29
|
-
trace: typeof console.trace;
|
|
30
|
-
};
|
|
65
|
+
getOriginalConsole(): SafeConsole;
|
|
31
66
|
/**
|
|
32
67
|
* Format console arguments into a single message
|
|
33
68
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"console-interceptor.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/console-interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"console-interceptor.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/console-interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AA0B/D;;;;;;;;;GASG;AACH,qBAAa,kBAAkB;IAiBjB,OAAO,CAAC,UAAU;IAhB9B;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,MAAM,CAAC,CAAc;IAE7B,OAAO,CAAC,cAAc,CAAS;gBAEX,UAAU,EAAE,WAAW;IAE3C;;;;;;;;OAQG;IACH,OAAO,CAAC,eAAe;IAavB;;OAEG;IACH,SAAS,IAAI,IAAI;IA0EjB;;;;;;;;;;;;;;OAcG;IACH,OAAO,IAAI,IAAI;IAmCf;;;;OAIG;IACH,kBAAkB,IAAI,WAAW;IAIjC;;OAEG;IACH,OAAO,CAAC,aAAa;CAiBtB"}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
import { safeConsole } from './safe-console.js';
|
|
2
|
+
const CONSOLE_METHODS = ['log', 'debug', 'info', 'warn', 'error', 'trace'];
|
|
3
|
+
/** Every patch currently installed, oldest first. */
|
|
4
|
+
const activePatches = [];
|
|
1
5
|
/**
|
|
2
6
|
* Console interceptor that routes console methods through the logging system
|
|
3
7
|
*
|
|
4
8
|
* By default, this is enabled. Set `interceptConsole: false` in LoggingPluginConfig to disable.
|
|
9
|
+
*
|
|
10
|
+
* The traffic is one-way: application `console.*` calls reach the logger, and
|
|
11
|
+
* the logger's own output goes to {@link safeConsole}, the copy captured
|
|
12
|
+
* before any interception. The console transport writing through the patched
|
|
13
|
+
* globals is what made the default configuration exhaust the heap (#85).
|
|
5
14
|
*/
|
|
6
15
|
export class ConsoleInterceptor {
|
|
7
16
|
constructor(logManager) {
|
|
@@ -11,7 +20,19 @@ export class ConsoleInterceptor {
|
|
|
11
20
|
writable: true,
|
|
12
21
|
value: logManager
|
|
13
22
|
});
|
|
14
|
-
|
|
23
|
+
/**
|
|
24
|
+
* This interceptor's live patch, while it is intercepting.
|
|
25
|
+
*
|
|
26
|
+
* `previous` is the globals as they were the moment `intercept()` patched
|
|
27
|
+
* them, so restoring is an exact undo rather than a reset to
|
|
28
|
+
* {@link safeConsole}: a host that wrapped `console` after importing this
|
|
29
|
+
* package (a breadcrumb hook, a test harness) keeps its wrapper.
|
|
30
|
+
*
|
|
31
|
+
* What the logging system WRITES through stays {@link safeConsole}, the
|
|
32
|
+
* module-load copy: an `intercept()`-time snapshot could capture another
|
|
33
|
+
* interceptor's patched methods and chain two loggers together (#85).
|
|
34
|
+
*/
|
|
35
|
+
Object.defineProperty(this, "record", {
|
|
15
36
|
enumerable: true,
|
|
16
37
|
configurable: true,
|
|
17
38
|
writable: true,
|
|
@@ -23,14 +44,25 @@ export class ConsoleInterceptor {
|
|
|
23
44
|
writable: true,
|
|
24
45
|
value: false
|
|
25
46
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The globals as they are right now, to be put back verbatim later.
|
|
50
|
+
*
|
|
51
|
+
* Captured WITHOUT binding: `restore()` reinstalls these as console methods,
|
|
52
|
+
* where `this` is the console again, and a caller comparing
|
|
53
|
+
* `console.log === theirs` after a restore expects the very function it
|
|
54
|
+
* installed — a bound copy would not be it. (The module-load
|
|
55
|
+
* {@link safeConsole} does bind, because it is called detached.)
|
|
56
|
+
*/
|
|
57
|
+
snapshotConsole() {
|
|
58
|
+
const at = (key) => typeof console[key] === 'function' ? console[key] : safeConsole[key];
|
|
59
|
+
return {
|
|
60
|
+
log: at('log'),
|
|
61
|
+
debug: at('debug'),
|
|
62
|
+
info: at('info'),
|
|
63
|
+
warn: at('warn'),
|
|
64
|
+
error: at('error'),
|
|
65
|
+
trace: at('trace'),
|
|
34
66
|
};
|
|
35
67
|
}
|
|
36
68
|
/**
|
|
@@ -40,62 +72,111 @@ export class ConsoleInterceptor {
|
|
|
40
72
|
if (this.isIntercepting) {
|
|
41
73
|
return;
|
|
42
74
|
}
|
|
75
|
+
// Snapshot before patching, so `restore()` is an exact undo.
|
|
76
|
+
const previous = this.snapshotConsole();
|
|
43
77
|
const logger = this.logManager.createLogger({
|
|
44
78
|
plugin: 'console-interceptor',
|
|
45
79
|
module: 'global'
|
|
46
80
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
63
|
-
// Intercept console.error
|
|
64
|
-
console.error = (...args) => {
|
|
65
|
-
const message = this.formatMessage(args);
|
|
66
|
-
const error = args.find(arg => arg instanceof Error);
|
|
67
|
-
logger.error(message, error);
|
|
81
|
+
const record = { patched: {}, previous, retired: false };
|
|
82
|
+
/**
|
|
83
|
+
* A patched method, which stops logging once this interceptor has
|
|
84
|
+
* restored. A wrapper installed over us keeps calling the function it
|
|
85
|
+
* captured; after we retire, that call must reach the console rather than
|
|
86
|
+
* a logger that has been shut down. `previous` is deliberately unbound, so
|
|
87
|
+
* it is invoked with `console` as the receiver — a detached call to a
|
|
88
|
+
* native console method throws `Illegal invocation` in Chromium.
|
|
89
|
+
*/
|
|
90
|
+
const via = (key, live) => (...args) => {
|
|
91
|
+
if (record.retired) {
|
|
92
|
+
record.previous[key].apply(console, args);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
live(...args);
|
|
68
96
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
97
|
+
record.patched = {
|
|
98
|
+
log: via('log', (...args) => {
|
|
99
|
+
logger.info(this.formatMessage(args));
|
|
100
|
+
}),
|
|
101
|
+
debug: via('debug', (...args) => {
|
|
102
|
+
logger.debug(this.formatMessage(args));
|
|
103
|
+
}),
|
|
104
|
+
info: via('info', (...args) => {
|
|
105
|
+
logger.info(this.formatMessage(args));
|
|
106
|
+
}),
|
|
107
|
+
warn: via('warn', (...args) => {
|
|
108
|
+
logger.warn(this.formatMessage(args));
|
|
109
|
+
}),
|
|
110
|
+
error: via('error', (...args) => {
|
|
111
|
+
const message = this.formatMessage(args);
|
|
112
|
+
const error = args.find(arg => arg instanceof Error);
|
|
113
|
+
logger.error(message, error);
|
|
114
|
+
}),
|
|
115
|
+
trace: via('trace', (...args) => {
|
|
116
|
+
const message = this.formatMessage(args);
|
|
117
|
+
logger.debug(message, {
|
|
118
|
+
stack: new Error().stack,
|
|
119
|
+
tags: ['trace']
|
|
120
|
+
});
|
|
121
|
+
}),
|
|
76
122
|
};
|
|
123
|
+
for (const key of CONSOLE_METHODS) {
|
|
124
|
+
console[key] = record.patched[key];
|
|
125
|
+
}
|
|
126
|
+
this.record = record;
|
|
127
|
+
activePatches.push(record);
|
|
77
128
|
this.isIntercepting = true;
|
|
78
129
|
}
|
|
79
130
|
/**
|
|
80
|
-
* Stop intercepting and restore original console methods
|
|
131
|
+
* Stop intercepting and restore original console methods.
|
|
132
|
+
*
|
|
133
|
+
* Interceptors nest — the plugin intercepts in `activate()` and restores in
|
|
134
|
+
* `deactivate()`, so two hosts can be active at once and may shut down in
|
|
135
|
+
* any order. Restoring by plain assignment is only correct for whoever is on
|
|
136
|
+
* top; an earlier interceptor doing it would reinstall a patch belonging to
|
|
137
|
+
* a logger that is being shut down, and every later `console.log` would
|
|
138
|
+
* disappear into it.
|
|
139
|
+
*
|
|
140
|
+
* So a restore UNLINKS instead: if we are on top, put our snapshot back;
|
|
141
|
+
* otherwise hand our snapshot to whoever patched over us, so their own
|
|
142
|
+
* restore reinstalls it. If neither holds — something outside this package
|
|
143
|
+
* owns the global now — we leave it alone rather than clobber it.
|
|
81
144
|
*/
|
|
82
145
|
restore() {
|
|
83
|
-
if (!this.isIntercepting) {
|
|
146
|
+
if (!this.isIntercepting || this.record === undefined) {
|
|
84
147
|
return;
|
|
85
148
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
149
|
+
const mine = this.record;
|
|
150
|
+
const { patched, previous } = mine;
|
|
151
|
+
// Anything still holding one of our functions — a wrapper installed above
|
|
152
|
+
// us, which the unlink below deliberately leaves in place — must stop
|
|
153
|
+
// reaching this logger. A retired patch forwards to what it replaced.
|
|
154
|
+
mine.retired = true;
|
|
155
|
+
for (const key of CONSOLE_METHODS) {
|
|
156
|
+
if (console[key] === patched[key]) {
|
|
157
|
+
console[key] = previous[key];
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const above = activePatches.find((other) => other !== mine && other.previous[key] === patched[key]);
|
|
161
|
+
if (above !== undefined) {
|
|
162
|
+
above.previous[key] = previous[key];
|
|
163
|
+
}
|
|
164
|
+
// else: a third party holds this method now; leave it untouched.
|
|
165
|
+
}
|
|
166
|
+
const index = activePatches.indexOf(mine);
|
|
167
|
+
if (index >= 0) {
|
|
168
|
+
activePatches.splice(index, 1);
|
|
169
|
+
}
|
|
170
|
+
this.record = undefined;
|
|
92
171
|
this.isIntercepting = false;
|
|
93
172
|
}
|
|
94
173
|
/**
|
|
95
|
-
*
|
|
174
|
+
* The console the logging system itself writes through: the copy captured at
|
|
175
|
+
* module load, bound to `console`, so callers may hold it detached. Never
|
|
176
|
+
* the patched globals.
|
|
96
177
|
*/
|
|
97
178
|
getOriginalConsole() {
|
|
98
|
-
return
|
|
179
|
+
return safeConsole;
|
|
99
180
|
}
|
|
100
181
|
/**
|
|
101
182
|
* Format console arguments into a single message
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-emitter.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/event-emitter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"event-emitter.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/event-emitter.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,SAAS,CAAoC;IAErD,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,GAAG,IAAI;IAO1C,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,GAAG,IAAI;IAU3C,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAazC,KAAK,IAAI,IAAI;CAGd"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { safeConsole } from './safe-console.js';
|
|
1
2
|
/**
|
|
2
3
|
* Simple event emitter for internal use
|
|
3
4
|
*/
|
|
@@ -33,7 +34,7 @@ export class EventEmitter {
|
|
|
33
34
|
handler(...args);
|
|
34
35
|
}
|
|
35
36
|
catch (error) {
|
|
36
|
-
|
|
37
|
+
safeConsole.error(`[EventEmitter] Error in handler for '${event}':`, error);
|
|
37
38
|
}
|
|
38
39
|
});
|
|
39
40
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC"}
|
package/dist/impl/utils/index.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface SafeConsole {
|
|
2
|
+
log: (...args: any[]) => void;
|
|
3
|
+
debug: (...args: any[]) => void;
|
|
4
|
+
info: (...args: any[]) => void;
|
|
5
|
+
warn: (...args: any[]) => void;
|
|
6
|
+
error: (...args: any[]) => void;
|
|
7
|
+
trace: (...args: any[]) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare const safeConsole: SafeConsole;
|
|
10
|
+
//# sourceMappingURL=safe-console.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-console.d.ts","sourceRoot":"","sources":["../../../src/impl/utils/safe-console.ts"],"names":[],"mappings":"AA2BA,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAC9B,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChC,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAC/B,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAC/B,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CACjC;AAED,eAAO,MAAM,WAAW,EAAE,WAUnB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The console the logging system writes its own output through.
|
|
3
|
+
*
|
|
4
|
+
* `ConsoleInterceptor` replaces the global console methods with ones that
|
|
5
|
+
* *log*, so that an application's own `console.log` reaches the logging
|
|
6
|
+
* pipeline. If the console transport then emitted through those same globals,
|
|
7
|
+
* every record it wrote would re-enter the logger and be written again —
|
|
8
|
+
* unbounded, one `JSON.stringify` per turn, until the process runs out of
|
|
9
|
+
* heap. That is amah/app-framework#85, and it killed any host using the
|
|
10
|
+
* plugin's default configuration.
|
|
11
|
+
*
|
|
12
|
+
* So the methods are captured once, at module load — before any interceptor
|
|
13
|
+
* can run, since interception happens in the plugin's `activate()` — and the
|
|
14
|
+
* transport writes through this copy. Application code reaching the logger and
|
|
15
|
+
* the logger reaching the real console then stay one-way.
|
|
16
|
+
*
|
|
17
|
+
* A console method patched by something else *before* this module is imported
|
|
18
|
+
* is captured as-is; there is no pristine console to recover at that point,
|
|
19
|
+
* and this only promises that the logging system does not re-enter itself.
|
|
20
|
+
*/
|
|
21
|
+
const bind = (method, fallback) => typeof method === 'function' ? method.bind(console) : fallback;
|
|
22
|
+
const noop = (() => {
|
|
23
|
+
/* a host without a console (some workers, some embedders) */
|
|
24
|
+
});
|
|
25
|
+
export const safeConsole = typeof console === 'undefined'
|
|
26
|
+
? { log: noop, debug: noop, info: noop, warn: noop, error: noop, trace: noop }
|
|
27
|
+
: {
|
|
28
|
+
log: bind(console.log, noop),
|
|
29
|
+
debug: bind(console.debug, noop),
|
|
30
|
+
info: bind(console.info, noop),
|
|
31
|
+
warn: bind(console.warn, noop),
|
|
32
|
+
error: bind(console.error, noop),
|
|
33
|
+
trace: bind(console.trace, noop),
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamak/logging",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "Pluggable logging system with API, SPI, and implementation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -45,7 +45,9 @@
|
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsc -p tsconfig.lib.json && node ../../scripts/fix-esm-extensions.mjs ./dist",
|
|
48
|
-
"clean": "rm -rf dist"
|
|
48
|
+
"clean": "rm -rf dist",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest"
|
|
49
51
|
},
|
|
50
52
|
"keywords": [
|
|
51
53
|
"logging",
|
|
@@ -55,8 +57,8 @@
|
|
|
55
57
|
"author": "",
|
|
56
58
|
"license": "MIT",
|
|
57
59
|
"dependencies": {
|
|
58
|
-
"@hamak/microkernel-api": "^0.12.
|
|
59
|
-
"@hamak/microkernel-spi": "^0.12.
|
|
60
|
+
"@hamak/microkernel-api": "^0.12.2",
|
|
61
|
+
"@hamak/microkernel-spi": "^0.12.2"
|
|
60
62
|
},
|
|
61
63
|
"devDependencies": {
|
|
62
64
|
"typescript": "^5.9.3",
|