@genesislcap/foundation-logger 15.46.5 → 15.46.6-alpha-6d1bd2f25.0
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 +23 -0
- package/dist/dts/global-reporters.d.ts +33 -0
- package/dist/dts/global-reporters.d.ts.map +1 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/logger.d.ts +1 -0
- package/dist/dts/logger.d.ts.map +1 -1
- package/dist/esm/global-reporters.js +109 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/logger.js +17 -4
- package/dist/foundation-logger.api.json +46 -0
- package/dist/foundation-logger.d.ts +27 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,29 @@ import { createLogger, TimestampBrowserReporter } from '@genesislcap/foundation-
|
|
|
37
37
|
createLogger('node-side', { reporters: [new TimestampBrowserReporter()] });
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
## Global log reporters
|
|
41
|
+
|
|
42
|
+
Genesis packages (`foundation-comms`, `foundation-state-machine`, etc.) report messages through their own `createLogger()` instances created at import time. Register a global reporter to observe logs from all foundation loggers across the application, including loggers instantiated before registration and loggers created by Module Federation remotes:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
|
|
46
|
+
|
|
47
|
+
const unsubscribe = addGlobalLogReporter({
|
|
48
|
+
log: ({ type, tag, args }) => {
|
|
49
|
+
if (type !== 'error') return;
|
|
50
|
+
const error = args.find((a) => a instanceof Error);
|
|
51
|
+
error
|
|
52
|
+
? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
|
|
53
|
+
: Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Remove the reporter when no longer needed:
|
|
58
|
+
unsubscribe();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The reporter registry is stored on `globalThis` via `Symbol.for`, so one registration covers every runtime copy of this package.
|
|
62
|
+
|
|
40
63
|
## Installation
|
|
41
64
|
|
|
42
65
|
Add the package to your `package.json` dependencies. After changing dependencies, run `npm run bootstrap` (or your project's equivalent). See [package.json basics](https://learn.genesis.global/secure/web/basics/package-json-basics/) for more information.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ConsolaInstance, ConsolaReporter } from 'consola/core';
|
|
2
|
+
/**
|
|
3
|
+
* Registers a logger instance with the global registry so it receives any currently
|
|
4
|
+
* active and future global log reporters.
|
|
5
|
+
*
|
|
6
|
+
* @param logger - The logger instance to register.
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare function registerLogger(logger: ConsolaInstance): void;
|
|
10
|
+
/**
|
|
11
|
+
* Registers a reporter that receives every log from every foundation logger, including loggers
|
|
12
|
+
* created before this call. Returns an unsubscribe function.
|
|
13
|
+
*
|
|
14
|
+
* @example Register an error monitor.
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
|
|
17
|
+
*
|
|
18
|
+
* const unsubscribe = addGlobalLogReporter({
|
|
19
|
+
* log: ({ type, tag, args }) => {
|
|
20
|
+
* if (type !== 'error') return;
|
|
21
|
+
* const error = args.find((a) => a instanceof Error);
|
|
22
|
+
* error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
|
|
23
|
+
* : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param reporter - The Consola reporter to register globally.
|
|
29
|
+
* @returns An unsubscribe function that removes the reporter from all loggers.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare function addGlobalLogReporter(reporter: ConsolaReporter): () => void;
|
|
33
|
+
//# sourceMappingURL=global-reporters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global-reporters.d.ts","sourceRoot":"","sources":["../../src/global-reporters.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAiDrE;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAU5D;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM,IAAI,CA6C1E"}
|
package/dist/dts/index.d.ts
CHANGED
package/dist/dts/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,8BAA8B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,cAAc,kBAAkB,CAAC;AACjC,cAAc,UAAU,CAAC;AACzB,cAAc,8BAA8B,CAAC"}
|
package/dist/dts/logger.d.ts
CHANGED
package/dist/dts/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EACf,cAAc,EAGf,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EACf,cAAc,EAGf,MAAM,cAAc,CAAC;AAKtB;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5D,YAAY,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD;;;;;;;;GAQG;AACH,MAAM,WAAW,aAAc,SAAQ,OAAO,CAAC,cAAc,CAAC;IAC5D;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,MAAO,SAAQ,eAAe;IAC7C;;;;;;;;;;;;;;;;;;;OAmBG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,oBAAoB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvF;AAED;;;GAGG;AACH,MAAM,WAAW,MAAO,SAAQ,eAAe;CAAG;AAElD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oBAAoB,EAAE,aAKlC,CAAC;AAgDF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAM9E"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared across every copy of this module (including Module Federation remotes that
|
|
3
|
+
* bundled their own `@genesislcap/foundation-logger`). Storing on `globalThis` via
|
|
4
|
+
* `Symbol.for` ensures that reporters registered in one copy receive logs from loggers
|
|
5
|
+
* created in any other copy.
|
|
6
|
+
*/
|
|
7
|
+
const GLOBAL_REPORTERS_KEY = Symbol.for('@genesislcap/foundation-logger:global-reporters');
|
|
8
|
+
const GLOBAL_LOGGERS_KEY = Symbol.for('@genesislcap/foundation-logger:global-loggers');
|
|
9
|
+
function createLoggerRef(logger) {
|
|
10
|
+
return typeof WeakRef !== 'undefined' ? new WeakRef(logger) : { deref: () => logger };
|
|
11
|
+
}
|
|
12
|
+
function getGlobalReporters() {
|
|
13
|
+
let reporters = Reflect.get(globalThis, GLOBAL_REPORTERS_KEY);
|
|
14
|
+
if (!reporters) {
|
|
15
|
+
reporters = new Map();
|
|
16
|
+
Reflect.set(globalThis, GLOBAL_REPORTERS_KEY, reporters);
|
|
17
|
+
}
|
|
18
|
+
return reporters;
|
|
19
|
+
}
|
|
20
|
+
function getGlobalLoggerRefs() {
|
|
21
|
+
let refs = Reflect.get(globalThis, GLOBAL_LOGGERS_KEY);
|
|
22
|
+
if (!refs) {
|
|
23
|
+
refs = new Set();
|
|
24
|
+
Reflect.set(globalThis, GLOBAL_LOGGERS_KEY, refs);
|
|
25
|
+
}
|
|
26
|
+
return refs;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Registers a logger instance with the global registry so it receives any currently
|
|
30
|
+
* active and future global log reporters.
|
|
31
|
+
*
|
|
32
|
+
* @param logger - The logger instance to register.
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export function registerLogger(logger) {
|
|
36
|
+
const refs = getGlobalLoggerRefs();
|
|
37
|
+
refs.add(createLoggerRef(logger));
|
|
38
|
+
const reporters = getGlobalReporters();
|
|
39
|
+
for (const reporter of reporters.keys()) {
|
|
40
|
+
if (!logger.options.reporters.includes(reporter)) {
|
|
41
|
+
logger.addReporter(reporter);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Registers a reporter that receives every log from every foundation logger, including loggers
|
|
47
|
+
* created before this call. Returns an unsubscribe function.
|
|
48
|
+
*
|
|
49
|
+
* @example Register an error monitor.
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
|
|
52
|
+
*
|
|
53
|
+
* const unsubscribe = addGlobalLogReporter({
|
|
54
|
+
* log: ({ type, tag, args }) => {
|
|
55
|
+
* if (type !== 'error') return;
|
|
56
|
+
* const error = args.find((a) => a instanceof Error);
|
|
57
|
+
* error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
|
|
58
|
+
* : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @param reporter - The Consola reporter to register globally.
|
|
64
|
+
* @returns An unsubscribe function that removes the reporter from all loggers.
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
export function addGlobalLogReporter(reporter) {
|
|
68
|
+
if (!reporter || typeof reporter.log !== 'function') {
|
|
69
|
+
return () => { };
|
|
70
|
+
}
|
|
71
|
+
const reporters = getGlobalReporters();
|
|
72
|
+
const currentCount = reporters.get(reporter) || 0;
|
|
73
|
+
reporters.set(reporter, currentCount + 1);
|
|
74
|
+
const refs = getGlobalLoggerRefs();
|
|
75
|
+
if (currentCount === 0) {
|
|
76
|
+
for (const ref of refs) {
|
|
77
|
+
const logger = ref.deref();
|
|
78
|
+
if (!logger) {
|
|
79
|
+
refs.delete(ref);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!logger.options.reporters.includes(reporter)) {
|
|
83
|
+
logger.addReporter(reporter);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let active = true;
|
|
88
|
+
return () => {
|
|
89
|
+
if (!active) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
active = false;
|
|
93
|
+
const count = (reporters.get(reporter) || 1) - 1;
|
|
94
|
+
if (count <= 0) {
|
|
95
|
+
reporters.delete(reporter);
|
|
96
|
+
for (const ref of refs) {
|
|
97
|
+
const logger = ref.deref();
|
|
98
|
+
if (!logger) {
|
|
99
|
+
refs.delete(ref);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
logger.removeReporter(reporter);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
reporters.set(reporter, count);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/logger.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createConsola as createConsolaBrowser } from 'consola/browser';
|
|
2
2
|
import { LogLevels, createConsola as createConsolaCore, } from 'consola/core';
|
|
3
|
+
import { registerLogger } from './global-reporters';
|
|
3
4
|
import { TimestampBrowserReporter } from './timestamp-browser-reporter';
|
|
4
5
|
/**
|
|
5
6
|
* Used to set the logging level.
|
|
@@ -45,6 +46,21 @@ function resolveLoggerOptions(options, isBrowser) {
|
|
|
45
46
|
}
|
|
46
47
|
return resolved;
|
|
47
48
|
}
|
|
49
|
+
function attachDeprecated(logger) {
|
|
50
|
+
logger.deprecated = (symbol, instruction = 'Consult docs for better alternative.', removalVersionTarget) => {
|
|
51
|
+
logger.warn(`Deprecated symbol used '${symbol}'. ${instruction}`, `${removalVersionTarget ? `Will be removed in version ${removalVersionTarget}.` : ''}`);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function configureLogger(logger) {
|
|
55
|
+
attachDeprecated(logger);
|
|
56
|
+
const originalWithTag = logger.withTag.bind(logger);
|
|
57
|
+
logger.withTag = (tag) => {
|
|
58
|
+
const child = originalWithTag(tag);
|
|
59
|
+
return configureLogger(child);
|
|
60
|
+
};
|
|
61
|
+
registerLogger(logger);
|
|
62
|
+
return logger;
|
|
63
|
+
}
|
|
48
64
|
/**
|
|
49
65
|
* Creates a logger with the given name and options.
|
|
50
66
|
*
|
|
@@ -88,8 +104,5 @@ export function createLogger(name, options = {}) {
|
|
|
88
104
|
const createConsola = isBrowser ? createConsolaBrowser : createConsolaCore;
|
|
89
105
|
const logger = createConsola(resolveLoggerOptions(options, isBrowser)).withTag(name);
|
|
90
106
|
const typedLogger = logger;
|
|
91
|
-
|
|
92
|
-
typedLogger.warn(`Deprecated symbol used '${symbol}'. ${instruction}`, `${removalVersionTarget ? `Will be removed in version ${removalVersionTarget}.` : ''}`);
|
|
93
|
-
};
|
|
94
|
-
return typedLogger;
|
|
107
|
+
return configureLogger(typedLogger);
|
|
95
108
|
}
|
|
@@ -172,6 +172,52 @@
|
|
|
172
172
|
"name": "",
|
|
173
173
|
"preserveMemberOrder": false,
|
|
174
174
|
"members": [
|
|
175
|
+
{
|
|
176
|
+
"kind": "Function",
|
|
177
|
+
"canonicalReference": "@genesislcap/foundation-logger!addGlobalLogReporter:function(1)",
|
|
178
|
+
"docComment": "/**\n * Registers a reporter that receives every log from every foundation logger, including loggers created before this call. Returns an unsubscribe function.\n *\n * @param reporter - The Consola reporter to register globally.\n *\n * @returns An unsubscribe function that removes the reporter from all loggers.\n *\n * @example\n *\n * Register an error monitor.\n * ```ts\n * import { addGlobalLogReporter } from '@genesislcap/foundation-logger';\n *\n * const unsubscribe = addGlobalLogReporter({\n * log: ({ type, tag, args }) => {\n * if (type !== 'error') return;\n * const error = args.find((a) => a instanceof Error);\n * error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })\n * : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });\n * },\n * });\n * ```\n *\n * @public\n */\n",
|
|
179
|
+
"excerptTokens": [
|
|
180
|
+
{
|
|
181
|
+
"kind": "Content",
|
|
182
|
+
"text": "export declare function addGlobalLogReporter(reporter: "
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"kind": "Reference",
|
|
186
|
+
"text": "ConsolaReporter",
|
|
187
|
+
"canonicalReference": "consola!ConsolaReporter:interface"
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
"kind": "Content",
|
|
191
|
+
"text": "): "
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
"kind": "Content",
|
|
195
|
+
"text": "() => void"
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
"kind": "Content",
|
|
199
|
+
"text": ";"
|
|
200
|
+
}
|
|
201
|
+
],
|
|
202
|
+
"fileUrlPath": "src/global-reporters.ts",
|
|
203
|
+
"returnTypeTokenRange": {
|
|
204
|
+
"startIndex": 3,
|
|
205
|
+
"endIndex": 4
|
|
206
|
+
},
|
|
207
|
+
"releaseTag": "Public",
|
|
208
|
+
"overloadIndex": 1,
|
|
209
|
+
"parameters": [
|
|
210
|
+
{
|
|
211
|
+
"parameterName": "reporter",
|
|
212
|
+
"parameterTypeTokenRange": {
|
|
213
|
+
"startIndex": 1,
|
|
214
|
+
"endIndex": 2
|
|
215
|
+
},
|
|
216
|
+
"isOptional": false
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
"name": "addGlobalLogReporter"
|
|
220
|
+
},
|
|
175
221
|
{
|
|
176
222
|
"kind": "Function",
|
|
177
223
|
"canonicalReference": "@genesislcap/foundation-logger!areLogTimestampsEnabled:function(1)",
|
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
import { ConsolaInstance } from 'consola/core';
|
|
2
2
|
import { ConsolaOptions } from 'consola/core';
|
|
3
|
-
import
|
|
3
|
+
import { ConsolaReporter } from 'consola/core';
|
|
4
4
|
import { LogLevel } from 'consola/core';
|
|
5
5
|
import { LogLevels } from 'consola/core';
|
|
6
6
|
import type { LogObject } from 'consola/core';
|
|
7
7
|
import { LogType } from 'consola/core';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Registers a reporter that receives every log from every foundation logger, including loggers
|
|
11
|
+
* created before this call. Returns an unsubscribe function.
|
|
12
|
+
*
|
|
13
|
+
* @example Register an error monitor.
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
|
|
16
|
+
*
|
|
17
|
+
* const unsubscribe = addGlobalLogReporter({
|
|
18
|
+
* log: ({ type, tag, args }) => {
|
|
19
|
+
* if (type !== 'error') return;
|
|
20
|
+
* const error = args.find((a) => a instanceof Error);
|
|
21
|
+
* error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
|
|
22
|
+
* : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param reporter - The Consola reporter to register globally.
|
|
28
|
+
* @returns An unsubscribe function that removes the reporter from all loggers.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export declare function addGlobalLogReporter(reporter: ConsolaReporter): () => void;
|
|
32
|
+
|
|
9
33
|
/**
|
|
10
34
|
* Returns whether the global timestamp prefix is currently enabled.
|
|
11
35
|
*
|
|
@@ -14,6 +38,8 @@ import { LogType } from 'consola/core';
|
|
|
14
38
|
*/
|
|
15
39
|
export declare function areLogTimestampsEnabled(): boolean;
|
|
16
40
|
|
|
41
|
+
export { ConsolaReporter }
|
|
42
|
+
|
|
17
43
|
/**
|
|
18
44
|
* Creates a logger with the given name and options.
|
|
19
45
|
*
|
package/package.json
CHANGED