@genesislcap/foundation-logger 15.47.0 → 15.48.0-alpha-830388687.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 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. Child loggers (`withTag`, `withScope`, `withDefaults`, `create`) are covered too. A reporter that throws is reported on the console instead of breaking the log call, and a logger created with the reporter in its own `reporters` keeps it after unsubscribing.
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,37 @@
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
+ * @remarks
15
+ * An error thrown by the reporter is written to the console instead of propagating out of the
16
+ * log call. Loggers that were created with the reporter keep it after unsubscribing.
17
+ *
18
+ * @example Register an error monitor.
19
+ * ```ts
20
+ * import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
21
+ *
22
+ * const unsubscribe = addGlobalLogReporter({
23
+ * log: ({ type, tag, args }) => {
24
+ * if (type !== 'error') return;
25
+ * const error = args.find((a) => a instanceof Error);
26
+ * error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
27
+ * : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
28
+ * },
29
+ * });
30
+ * ```
31
+ *
32
+ * @param reporter - The Consola reporter to register globally.
33
+ * @returns An unsubscribe function that removes the reporter from all loggers.
34
+ * @public
35
+ */
36
+ export declare function addGlobalLogReporter(reporter: ConsolaReporter): () => void;
37
+ //# 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;AA8FrE;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAK5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM,IAAI,CA4B1E"}
@@ -1,3 +1,4 @@
1
+ export { addGlobalLogReporter } from './global-reporters';
1
2
  export * from './log-timestamps';
2
3
  export * from './logger';
3
4
  export * from './timestamp-browser-reporter';
@@ -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"}
@@ -12,6 +12,7 @@ import { ConsolaInstance, ConsolaOptions } from 'consola/core';
12
12
  * @public
13
13
  */
14
14
  export { LogLevel, LogLevels, LogType } from 'consola/core';
15
+ export type { ConsolaReporter } from 'consola/core';
15
16
  /**
16
17
  * Options for creating a logger.
17
18
  *
@@ -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;AAItB;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5D;;;;;;;;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;AAwBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAgB9E"}
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;AA8CF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAM9E"}
@@ -0,0 +1,124 @@
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
+ function guardReporter(reporter) {
29
+ return {
30
+ log: (logObj, ctx) => {
31
+ try {
32
+ reporter.log(logObj, ctx);
33
+ }
34
+ catch (error) {
35
+ // The unwrapped console, so a consola that has wrapped `console` can't loop back here.
36
+ const { __error, error: consoleError } = console;
37
+ (__error || consoleError)('A global log reporter threw', error);
38
+ }
39
+ },
40
+ };
41
+ }
42
+ function attachReporter(logger, reporter, { guarded }) {
43
+ const { reporters } = logger.options;
44
+ if (!reporters.includes(reporter) && !reporters.includes(guarded)) {
45
+ logger.addReporter(guarded);
46
+ }
47
+ }
48
+ function forEachLiveLogger(refs, callback) {
49
+ for (const ref of refs) {
50
+ const logger = ref.deref();
51
+ if (logger) {
52
+ callback(logger);
53
+ }
54
+ else {
55
+ refs.delete(ref);
56
+ }
57
+ }
58
+ }
59
+ /**
60
+ * Registers a logger instance with the global registry so it receives any currently
61
+ * active and future global log reporters.
62
+ *
63
+ * @param logger - The logger instance to register.
64
+ * @internal
65
+ */
66
+ export function registerLogger(logger) {
67
+ getGlobalLoggerRefs().add(createLoggerRef(logger));
68
+ for (const [reporter, registration] of getGlobalReporters()) {
69
+ attachReporter(logger, reporter, registration);
70
+ }
71
+ }
72
+ /**
73
+ * Registers a reporter that receives every log from every foundation logger, including loggers
74
+ * created before this call. Returns an unsubscribe function.
75
+ *
76
+ * @remarks
77
+ * An error thrown by the reporter is written to the console instead of propagating out of the
78
+ * log call. Loggers that were created with the reporter keep it after unsubscribing.
79
+ *
80
+ * @example Register an error monitor.
81
+ * ```ts
82
+ * import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
83
+ *
84
+ * const unsubscribe = addGlobalLogReporter({
85
+ * log: ({ type, tag, args }) => {
86
+ * if (type !== 'error') return;
87
+ * const error = args.find((a) => a instanceof Error);
88
+ * error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
89
+ * : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
90
+ * },
91
+ * });
92
+ * ```
93
+ *
94
+ * @param reporter - The Consola reporter to register globally.
95
+ * @returns An unsubscribe function that removes the reporter from all loggers.
96
+ * @public
97
+ */
98
+ export function addGlobalLogReporter(reporter) {
99
+ var _a;
100
+ if (!reporter || typeof reporter.log !== 'function') {
101
+ return () => { };
102
+ }
103
+ const reporters = getGlobalReporters();
104
+ const refs = getGlobalLoggerRefs();
105
+ const registration = (_a = reporters.get(reporter)) !== null && _a !== void 0 ? _a : { count: 0, guarded: guardReporter(reporter) };
106
+ registration.count += 1;
107
+ if (registration.count === 1) {
108
+ reporters.set(reporter, registration);
109
+ forEachLiveLogger(refs, (logger) => attachReporter(logger, reporter, registration));
110
+ }
111
+ let active = true;
112
+ return () => {
113
+ if (!active) {
114
+ return;
115
+ }
116
+ active = false;
117
+ registration.count -= 1;
118
+ if (registration.count > 0) {
119
+ return;
120
+ }
121
+ reporters.delete(reporter);
122
+ forEachLiveLogger(refs, (logger) => logger.removeReporter(registration.guarded));
123
+ };
124
+ }
package/dist/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { addGlobalLogReporter } from './global-reporters';
1
2
  export * from './log-timestamps';
2
3
  export * from './logger';
3
4
  export * from './timestamp-browser-reporter';
@@ -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,19 @@ 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 createChild = logger.create.bind(logger);
57
+ // consola's withTag, withScope and withDefaults all build their child through `this.create`.
58
+ logger.create = (options) => configureLogger(createChild(options));
59
+ registerLogger(logger);
60
+ return logger;
61
+ }
48
62
  /**
49
63
  * Creates a logger with the given name and options.
50
64
  *
@@ -88,8 +102,5 @@ export function createLogger(name, options = {}) {
88
102
  const createConsola = isBrowser ? createConsolaBrowser : createConsolaCore;
89
103
  const logger = createConsola(resolveLoggerOptions(options, isBrowser)).withTag(name);
90
104
  const typedLogger = logger;
91
- typedLogger.deprecated = (symbol, instruction = 'Consult docs for better alternative.', removalVersionTarget) => {
92
- typedLogger.warn(`Deprecated symbol used '${symbol}'. ${instruction}`, `${removalVersionTarget ? `Will be removed in version ${removalVersionTarget}.` : ''}`);
93
- };
94
- return typedLogger;
105
+ return configureLogger(typedLogger);
95
106
  }
@@ -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 * @remarks\n *\n * An error thrown by the reporter is written to the console instead of propagating out of the log call. Loggers that were created with the reporter keep it after unsubscribing.\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,39 @@
1
1
  import { ConsolaInstance } from 'consola/core';
2
2
  import { ConsolaOptions } from 'consola/core';
3
- import type { ConsolaReporter } from 'consola/core';
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
+ * @remarks
14
+ * An error thrown by the reporter is written to the console instead of propagating out of the
15
+ * log call. Loggers that were created with the reporter keep it after unsubscribing.
16
+ *
17
+ * @example Register an error monitor.
18
+ * ```ts
19
+ * import { addGlobalLogReporter } from '@genesislcap/foundation-logger';
20
+ *
21
+ * const unsubscribe = addGlobalLogReporter({
22
+ * log: ({ type, tag, args }) => {
23
+ * if (type !== 'error') return;
24
+ * const error = args.find((a) => a instanceof Error);
25
+ * error ? Sentry.captureException(error, { tags: { logger: tag }, extra: { args } })
26
+ * : Sentry.captureMessage(`${tag}: ${String(args[0])}`, { level: 'error', extra: { args } });
27
+ * },
28
+ * });
29
+ * ```
30
+ *
31
+ * @param reporter - The Consola reporter to register globally.
32
+ * @returns An unsubscribe function that removes the reporter from all loggers.
33
+ * @public
34
+ */
35
+ export declare function addGlobalLogReporter(reporter: ConsolaReporter): () => void;
36
+
9
37
  /**
10
38
  * Returns whether the global timestamp prefix is currently enabled.
11
39
  *
@@ -14,6 +42,8 @@ import { LogType } from 'consola/core';
14
42
  */
15
43
  export declare function areLogTimestampsEnabled(): boolean;
16
44
 
45
+ export { ConsolaReporter }
46
+
17
47
  /**
18
48
  * Creates a logger with the given name and options.
19
49
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/foundation-logger",
3
3
  "description": "Genesis Foundation Logger",
4
- "version": "15.47.0",
4
+ "version": "15.48.0-alpha-830388687.0",
5
5
  "sideEffects": false,
6
6
  "license": "SEE LICENSE IN license.txt",
7
7
  "main": "dist/esm/index.js",