-
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
146 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,25 @@ | ||
'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()) | ||
|
||
exports.captureLogs = function (onLog, fn) { | ||
return asyncStorage.run(onLog, fn) | ||
} | ||
|
||
exports.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