-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): helper to capture logs in an async stack
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
// Even though the lib is compatible with Node >=8.3, | ||
// the capture feature requires Node >=13.10 | ||
// | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
const { AsyncLocalStorage } = require('node:async_hooks') | ||
|
||
const createTransport = require('./_createTransport.js') | ||
|
||
// stored in the global context so that various versions of the library can interact. | ||
const symbol = Symbol.for('@xen-orchestra/log/capture') | ||
const asyncStorage = global[symbol] || (global[symbol] = new AsyncLocalStorage()) | ||
|
||
/** | ||
* Runs `fn` capturing all emitted logs (sync and async) and forward them to `onLog`. | ||
* | ||
* @template {(...args: any) => any} F | ||
* @param {undefined | (log: object) => void} onLog | ||
* @param {F} fn | ||
* @returns {ReturnType<F>} | ||
*/ | ||
exports.captureLogs = function captureLogs(onLog, fn) { | ||
return asyncStorage.run(onLog, fn) | ||
} | ||
|
||
/** | ||
* Creates a transport for the `captureLogs` feature. | ||
* | ||
* @param {*} fallback - The transport to use as a fallback when not capturing | ||
* @returns {(log: object) => void} | ||
*/ | ||
exports.createCaptureTransport = function createCaptureTransport(fallback) { | ||
fallback = fallback === undefined ? Function.prototype : createTransport(fallback) | ||
|
||
return function captureTransport(log) { | ||
;(asyncStorage.getStore() || fallback)(log) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters