@api3/commons 0.3.0 → 0.4.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/dist/logger/async-storage.d.ts +5 -0
- package/dist/logger/async-storage.d.ts.map +1 -0
- package/dist/logger/async-storage.js +12 -0
- package/dist/logger/async-storage.js.map +1 -0
- package/dist/logger/index.d.ts +5 -1
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +37 -10
- package/dist/logger/index.js.map +1 -1
- package/package.json +1 -1
- package/src/logger/async-storage.ts +10 -0
- package/src/logger/index.test.ts +111 -0
- package/src/logger/index.ts +40 -11
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-storage.d.ts","sourceRoot":"","sources":["../../src/logger/async-storage.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC;AAIpC,eAAO,MAAM,oBAAoB,qCAGhC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAsyncLocalStorage = void 0;
|
|
4
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
let asyncLocalStorage;
|
|
6
|
+
const getAsyncLocalStorage = () => {
|
|
7
|
+
if (!asyncLocalStorage)
|
|
8
|
+
asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
|
|
9
|
+
return asyncLocalStorage;
|
|
10
|
+
};
|
|
11
|
+
exports.getAsyncLocalStorage = getAsyncLocalStorage;
|
|
12
|
+
//# sourceMappingURL=async-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-storage.js","sourceRoot":"","sources":["../../src/logger/async-storage.ts"],"names":[],"mappings":";;;AAAA,uDAAqD;AAIrD,IAAI,iBAAgD,CAAC;AAE9C,MAAM,oBAAoB,GAAG,GAAG,EAAE;IACvC,IAAI,CAAC,iBAAiB;QAAE,iBAAiB,GAAG,IAAI,oCAAiB,EAAc,CAAC;IAChF,OAAO,iBAAiB,CAAC;AAC3B,CAAC,CAAC;AAHW,QAAA,oBAAoB,wBAG/B"}
|
package/dist/logger/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
1
2
|
export declare const logFormatOptions: readonly ["json", "pretty"];
|
|
2
3
|
export type LogFormat = (typeof logFormatOptions)[number];
|
|
3
4
|
export declare const logLevelOptions: readonly ["debug", "info", "warn", "error"];
|
|
@@ -8,16 +9,19 @@ export interface LogConfig {
|
|
|
8
9
|
format: LogFormat;
|
|
9
10
|
minLevel: LogLevel;
|
|
10
11
|
}
|
|
12
|
+
export declare const createBaseLogger: (config: LogConfig) => winston.Logger;
|
|
11
13
|
export type LogContext = Record<string, any>;
|
|
12
14
|
export interface Logger {
|
|
15
|
+
runWithContext: <T>(context: LogContext, fn: () => T) => T;
|
|
13
16
|
debug: (message: string, context?: LogContext) => void;
|
|
14
17
|
info: (message: string, context?: LogContext) => void;
|
|
15
18
|
warn: (message: string, context?: LogContext) => void;
|
|
16
|
-
error: ((message: string, context?: LogContext) => void)
|
|
19
|
+
error: ((message: string, context?: LogContext) => void) | ((message: string, error: Error, context?: LogContext) => void);
|
|
17
20
|
child: (options: {
|
|
18
21
|
name: string;
|
|
19
22
|
}) => Logger;
|
|
20
23
|
}
|
|
24
|
+
export declare const wrapper: (logger: winston.Logger) => Logger;
|
|
21
25
|
export declare const validateLogConfig: (config: unknown) => LogConfig;
|
|
22
26
|
export declare const createLogger: (config: LogConfig) => Logger;
|
|
23
27
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts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E,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC;CAC9C;AAID,eAAO,MAAM,OAAO,WAAY,cAAc,KAAG,MAyChD,CAAC;AAEF,eAAO,MAAM,iBAAiB,WAAY,OAAO,KAAG,SAuBnD,CAAC;AAEF,eAAO,MAAM,YAAY,WAAY,SAAS,WAG7C,CAAC"}
|
package/dist/logger/index.js
CHANGED
|
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createLogger = exports.validateLogConfig = exports.logLevelOptions = exports.logFormatOptions = void 0;
|
|
6
|
+
exports.createLogger = exports.validateLogConfig = exports.wrapper = exports.createBaseLogger = exports.logLevelOptions = exports.logFormatOptions = void 0;
|
|
7
7
|
const winston_1 = __importDefault(require("winston"));
|
|
8
8
|
const winston_console_format_1 = require("winston-console-format");
|
|
9
|
+
const async_storage_1 = require("./async-storage");
|
|
9
10
|
exports.logFormatOptions = ['json', 'pretty'];
|
|
10
11
|
exports.logLevelOptions = ['debug', 'info', 'warn', 'error'];
|
|
11
12
|
const createConsoleTransport = (config) => {
|
|
@@ -50,26 +51,52 @@ const createBaseLogger = (config) => {
|
|
|
50
51
|
transports: [createConsoleTransport(config)],
|
|
51
52
|
});
|
|
52
53
|
};
|
|
54
|
+
exports.createBaseLogger = createBaseLogger;
|
|
53
55
|
// Winston by default merges content of `context` among the rest of the fields for the JSON format.
|
|
54
56
|
// That's causing an override of fields `name` and `message` if they are present.
|
|
55
57
|
const wrapper = (logger) => {
|
|
56
58
|
return {
|
|
57
|
-
debug: (message,
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
debug: (message, localContext) => {
|
|
60
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
61
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
62
|
+
logger.debug(message, fullContext);
|
|
63
|
+
},
|
|
64
|
+
info: (message, localContext) => {
|
|
65
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
66
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
67
|
+
logger.info(message, fullContext);
|
|
68
|
+
},
|
|
69
|
+
warn: (message, localContext) => {
|
|
70
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
71
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
72
|
+
logger.warn(message, fullContext);
|
|
73
|
+
},
|
|
60
74
|
// We need to handle both overloads of the `error` function
|
|
61
|
-
error: (message,
|
|
75
|
+
error: (message, errorOrLocalContext, localContext) => {
|
|
76
|
+
const globalContext = (0, async_storage_1.getAsyncLocalStorage)().getStore();
|
|
62
77
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
63
|
-
if (
|
|
64
|
-
|
|
78
|
+
if (errorOrLocalContext instanceof Error) {
|
|
79
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
80
|
+
logger.error(message, errorOrLocalContext, fullContext);
|
|
65
81
|
}
|
|
66
82
|
else {
|
|
67
|
-
|
|
83
|
+
const fullContext = globalContext || errorOrLocalContext ? { ...globalContext, ...errorOrLocalContext } : undefined;
|
|
84
|
+
logger.error(message, fullContext);
|
|
68
85
|
}
|
|
69
86
|
},
|
|
70
|
-
child: (options) => wrapper(logger.child(options)),
|
|
87
|
+
child: (options) => (0, exports.wrapper)(logger.child(options)),
|
|
88
|
+
runWithContext: (context, fn) => {
|
|
89
|
+
const asyncStorage = (0, async_storage_1.getAsyncLocalStorage)();
|
|
90
|
+
const oldContext = asyncStorage.getStore() ?? {};
|
|
91
|
+
// From https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args
|
|
92
|
+
//
|
|
93
|
+
// If the callback function throws an error, the error is thrown by run() too. The stacktrace is not impacted by
|
|
94
|
+
// this call and the context is exited.
|
|
95
|
+
return asyncStorage.run({ ...oldContext, ...context }, fn);
|
|
96
|
+
},
|
|
71
97
|
};
|
|
72
98
|
};
|
|
99
|
+
exports.wrapper = wrapper;
|
|
73
100
|
const validateLogConfig = (config) => {
|
|
74
101
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
75
102
|
if (typeof config !== 'object' || config === null) {
|
|
@@ -95,7 +122,7 @@ const validateLogConfig = (config) => {
|
|
|
95
122
|
exports.validateLogConfig = validateLogConfig;
|
|
96
123
|
const createLogger = (config) => {
|
|
97
124
|
// Ensure that the logger configuration is valid.
|
|
98
|
-
return wrapper(createBaseLogger((0, exports.validateLogConfig)(config)));
|
|
125
|
+
return (0, exports.wrapper)((0, exports.createBaseLogger)((0, exports.validateLogConfig)(config)));
|
|
99
126
|
};
|
|
100
127
|
exports.createLogger = createLogger;
|
|
101
128
|
//# sourceMappingURL=index.js.map
|
package/dist/logger/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAC9B,mEAAuD;AAE1C,QAAA,gBAAgB,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAU,CAAC;AAI/C,QAAA,eAAe,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAU,CAAC;AAW3E,MAAM,sBAAsB,GAAG,CAAC,MAAiB,EAAE,EAAE;IACnD,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAE7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;KACzD;IAED,QAAQ,MAAM,EAAE;QACd,KAAK,MAAM,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C,IAAA,wBAAgB,EAAC,IAAA,yBAAiB,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC,CAAC;AAHW,QAAA,YAAY,gBAGvB"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
import type { LogContext } from '.';
|
|
4
|
+
|
|
5
|
+
let asyncLocalStorage: AsyncLocalStorage<LogContext>;
|
|
6
|
+
|
|
7
|
+
export const getAsyncLocalStorage = () => {
|
|
8
|
+
if (!asyncLocalStorage) asyncLocalStorage = new AsyncLocalStorage<LogContext>();
|
|
9
|
+
return asyncLocalStorage;
|
|
10
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { noop } from 'lodash';
|
|
2
|
+
|
|
3
|
+
import { type LogConfig, createBaseLogger, wrapper } from '.';
|
|
4
|
+
|
|
5
|
+
const createTestLogger = (
|
|
6
|
+
logConfig: LogConfig = { enabled: true, minLevel: 'debug', format: 'json', colorize: false }
|
|
7
|
+
) => {
|
|
8
|
+
const baseLogger = createBaseLogger(logConfig);
|
|
9
|
+
const logger = wrapper(baseLogger);
|
|
10
|
+
jest.spyOn(baseLogger, 'debug').mockImplementation(noop as any);
|
|
11
|
+
jest.spyOn(baseLogger, 'info').mockImplementation(noop as any);
|
|
12
|
+
jest.spyOn(baseLogger, 'warn').mockImplementation(noop as any);
|
|
13
|
+
jest.spyOn(baseLogger, 'error').mockImplementation(noop as any);
|
|
14
|
+
jest.spyOn(baseLogger, 'child').mockImplementation(noop as any);
|
|
15
|
+
|
|
16
|
+
return { baseLogger, logger };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
describe('log context', () => {
|
|
20
|
+
it('works with sync functions', () => {
|
|
21
|
+
const { baseLogger, logger } = createTestLogger();
|
|
22
|
+
|
|
23
|
+
logger.runWithContext({ requestId: 'parent' }, () => {
|
|
24
|
+
logger.debug('parent start');
|
|
25
|
+
logger.runWithContext({ requestId: 'child' }, () => {
|
|
26
|
+
logger.debug('child');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
logger.debug('parent end');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { requestId: 'parent' });
|
|
33
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('child', { requestId: 'child' });
|
|
34
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { requestId: 'parent' });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('works with async functions', async () => {
|
|
38
|
+
const { baseLogger, logger } = createTestLogger();
|
|
39
|
+
|
|
40
|
+
await logger.runWithContext({ requestId: 'parent' }, async () => {
|
|
41
|
+
logger.debug('parent start');
|
|
42
|
+
await logger.runWithContext({ requestId: 'child' }, async () => {
|
|
43
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
44
|
+
logger.debug('child');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
logger.debug('parent end');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(baseLogger.debug).toHaveBeenCalledTimes(3);
|
|
51
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { requestId: 'parent' });
|
|
52
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('child', { requestId: 'child' });
|
|
53
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { requestId: 'parent' });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('works with deeply nested functions', async () => {
|
|
57
|
+
const { baseLogger, logger } = createTestLogger();
|
|
58
|
+
|
|
59
|
+
await logger.runWithContext({ parent: true }, async () => {
|
|
60
|
+
logger.debug('parent start');
|
|
61
|
+
|
|
62
|
+
await logger.runWithContext({ A: true }, async () => {
|
|
63
|
+
logger.debug('A start');
|
|
64
|
+
|
|
65
|
+
await logger.runWithContext({ B: true }, async () => {
|
|
66
|
+
setTimeout(() => logger.debug('C'), 25);
|
|
67
|
+
setTimeout(() => logger.debug('D'), 50);
|
|
68
|
+
setTimeout(() => logger.debug('E'), 75);
|
|
69
|
+
|
|
70
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
71
|
+
logger.debug('B end');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
logger.debug('A end');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
logger.debug('parent end');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
expect(baseLogger.debug).toHaveBeenCalledTimes(8);
|
|
81
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent start', { parent: true });
|
|
82
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('A start', { parent: true, A: true });
|
|
83
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('C', { parent: true, A: true, B: true });
|
|
84
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('D', { parent: true, A: true, B: true });
|
|
85
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('E', { parent: true, A: true, B: true });
|
|
86
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('B end', { parent: true, A: true, B: true });
|
|
87
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('A end', { parent: true, A: true });
|
|
88
|
+
expect(baseLogger.debug).toHaveBeenCalledWith('parent end', { parent: true });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('throws if the sync callback function throws', () => {
|
|
92
|
+
const { logger } = createTestLogger();
|
|
93
|
+
|
|
94
|
+
expect(() =>
|
|
95
|
+
logger.runWithContext({}, () => {
|
|
96
|
+
throw new Error('some-error');
|
|
97
|
+
})
|
|
98
|
+
).toThrow('some-error');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('returns rejected promise if the async callback function rejects', async () => {
|
|
102
|
+
const { logger } = createTestLogger();
|
|
103
|
+
|
|
104
|
+
await expect(async () =>
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
106
|
+
logger.runWithContext({}, async () => {
|
|
107
|
+
throw new Error('some-error');
|
|
108
|
+
})
|
|
109
|
+
).rejects.toThrow('some-error');
|
|
110
|
+
});
|
|
111
|
+
});
|
package/src/logger/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import winston from 'winston';
|
|
2
2
|
import { consoleFormat } from 'winston-console-format';
|
|
3
3
|
|
|
4
|
+
import { getAsyncLocalStorage } from './async-storage';
|
|
5
|
+
|
|
4
6
|
export const logFormatOptions = ['json', 'pretty'] as const;
|
|
5
7
|
|
|
6
8
|
export type LogFormat = (typeof logFormatOptions)[number];
|
|
@@ -51,7 +53,7 @@ const createConsoleTransport = (config: LogConfig) => {
|
|
|
51
53
|
}
|
|
52
54
|
};
|
|
53
55
|
|
|
54
|
-
const createBaseLogger = (config: LogConfig) => {
|
|
56
|
+
export const createBaseLogger = (config: LogConfig) => {
|
|
55
57
|
const { enabled, minLevel } = config;
|
|
56
58
|
|
|
57
59
|
return winston.createLogger({
|
|
@@ -73,31 +75,58 @@ const createBaseLogger = (config: LogConfig) => {
|
|
|
73
75
|
export type LogContext = Record<string, any>;
|
|
74
76
|
|
|
75
77
|
export interface Logger {
|
|
78
|
+
runWithContext: <T>(context: LogContext, fn: () => T) => T;
|
|
76
79
|
debug: (message: string, context?: LogContext) => void;
|
|
77
80
|
info: (message: string, context?: LogContext) => void;
|
|
78
81
|
warn: (message: string, context?: LogContext) => void;
|
|
79
|
-
error:
|
|
80
|
-
((message: string,
|
|
82
|
+
error:
|
|
83
|
+
| ((message: string, context?: LogContext) => void)
|
|
84
|
+
| ((message: string, error: Error, context?: LogContext) => void);
|
|
81
85
|
child: (options: { name: string }) => Logger;
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
// Winston by default merges content of `context` among the rest of the fields for the JSON format.
|
|
85
89
|
// That's causing an override of fields `name` and `message` if they are present.
|
|
86
|
-
const wrapper = (logger: Logger): Logger => {
|
|
90
|
+
export const wrapper = (logger: winston.Logger): Logger => {
|
|
87
91
|
return {
|
|
88
|
-
debug: (message,
|
|
89
|
-
|
|
90
|
-
|
|
92
|
+
debug: (message, localContext) => {
|
|
93
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
94
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
95
|
+
logger.debug(message, fullContext);
|
|
96
|
+
},
|
|
97
|
+
info: (message, localContext) => {
|
|
98
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
99
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
100
|
+
logger.info(message, fullContext);
|
|
101
|
+
},
|
|
102
|
+
warn: (message, localContext) => {
|
|
103
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
104
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
105
|
+
logger.warn(message, fullContext);
|
|
106
|
+
},
|
|
91
107
|
// We need to handle both overloads of the `error` function
|
|
92
|
-
error: (message,
|
|
108
|
+
error: (message, errorOrLocalContext, localContext) => {
|
|
109
|
+
const globalContext = getAsyncLocalStorage().getStore();
|
|
93
110
|
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
94
|
-
if (
|
|
95
|
-
|
|
111
|
+
if (errorOrLocalContext instanceof Error) {
|
|
112
|
+
const fullContext = globalContext || localContext ? { ...globalContext, ...localContext } : undefined;
|
|
113
|
+
logger.error(message, errorOrLocalContext, fullContext);
|
|
96
114
|
} else {
|
|
97
|
-
|
|
115
|
+
const fullContext =
|
|
116
|
+
globalContext || errorOrLocalContext ? { ...globalContext, ...(errorOrLocalContext as any) } : undefined;
|
|
117
|
+
logger.error(message, fullContext);
|
|
98
118
|
}
|
|
99
119
|
},
|
|
100
120
|
child: (options) => wrapper(logger.child(options)),
|
|
121
|
+
runWithContext: (context, fn) => {
|
|
122
|
+
const asyncStorage = getAsyncLocalStorage();
|
|
123
|
+
const oldContext = asyncStorage.getStore() ?? {};
|
|
124
|
+
// From https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args
|
|
125
|
+
//
|
|
126
|
+
// If the callback function throws an error, the error is thrown by run() too. The stacktrace is not impacted by
|
|
127
|
+
// this call and the context is exited.
|
|
128
|
+
return asyncStorage.run({ ...oldContext, ...context }, fn);
|
|
129
|
+
},
|
|
101
130
|
} as Logger;
|
|
102
131
|
};
|
|
103
132
|
|