diff --git a/@xen-orchestra/log/.USAGE.md b/@xen-orchestra/log/.USAGE.md index 5a8e92824b7..1b139eb876d 100644 --- a/@xen-orchestra/log/.USAGE.md +++ b/@xen-orchestra/log/.USAGE.md @@ -192,3 +192,60 @@ app ERROR Something went wrong app ERROR duplicates of the previous log were hidden { nDuplicates: 2 } app INFO This is a different message ``` + +#### Capture + +> Allow capturing all logs emitted during a call, even through asynchronous operations. + +Before being able to use this feature, you need to add the transport: + +```js +import { configure } from '@xen-orchestra/log/configure' +import { createCaptureTransport } from '@xen-orchestra/log/capture' +import createConsoleTransport from '@xen-orchestra/log/transports/console' + +// transport that will be used globally, when not in a captured environment +const fallbackTransport = { + filter: process.env.DEBUG, + level: 'warn', + + transport: createConsoleTransport(), +} + +// create the capture transport and pass it the fallback one +const captureTransport = createCaptureTransport(fallbackTransport) + +// configure @xen-orchestra/log to use our transport +configure(captureTransport) +``` + +Now the `captureLogs(onLog, fn)` can be used: + +```js +import { captureLogs } from '@xen-orchestra/log/capture' +import { createLogger } from '@xen-orchestra/log' + +const logger = createLogger('my-logger') + +await captureLogs( + log => { + // every logs emitted in the async context of `fn` will arrive here + // + // do not emit logs in this function + }, + async () => { + logger.debug('synchronous logs are captured') + + setTimeout(() => { + logger.debug('logs from asynchronous callbacks too') + }, 50) + + await new Promise(resolve => setTimeout(resolve, 50)) + + logger.debug('logs in async functions or promise chains too') + + // Returned value and error is forwarded by `captureLogs` + return Math.PI + } +) +``` diff --git a/@xen-orchestra/log/README.md b/@xen-orchestra/log/README.md index 6dd4a0fe1f2..46afeb82c50 100644 --- a/@xen-orchestra/log/README.md +++ b/@xen-orchestra/log/README.md @@ -211,6 +211,63 @@ app ERROR duplicates of the previous log were hidden { nDuplicates: 2 } app INFO This is a different message ``` +#### Capture + +> Allow capturing all logs emitted during a call, even through asynchronous operations. + +Before being able to use this feature, you need to add the transport: + +```js +import { configure } from '@xen-orchestra/log/configure' +import { createCaptureTransport } from '@xen-orchestra/log/capture' +import createConsoleTransport from '@xen-orchestra/log/transports/console' + +// transport that will be used globally, when not in a captured environment +const fallbackTransport = { + filter: process.env.DEBUG, + level: 'warn', + + transport: createConsoleTransport(), +} + +// create the capture transport and pass it the fallback one +const captureTransport = createCaptureTransport(fallbackTransport) + +// configure @xen-orchestra/log to use our transport +configure(captureTransport) +``` + +Now the `captureLogs(onLog, fn)` can be used: + +```js +import { captureLogs } from '@xen-orchestra/log/capture' +import { createLogger } from '@xen-orchestra/log' + +const logger = createLogger('my-logger') + +await captureLogs( + log => { + // every logs emitted in the async context of `fn` will arrive here + // + // do not emit logs in this function + }, + async () => { + logger.debug('synchronous logs are captured') + + setTimeout(() => { + logger.debug('logs from asynchronous callbacks too') + }, 50) + + await new Promise(resolve => setTimeout(resolve, 50)) + + logger.debug('logs in async functions or promise chains too') + + // Returned value and error is forwarded by `captureLogs` + return Math.PI + } +) +``` + ## Contributions Contributions are _very_ welcomed, either on the documentation or on diff --git a/@xen-orchestra/log/capture.js b/@xen-orchestra/log/capture.js new file mode 100644 index 00000000000..a011b2190f2 --- /dev/null +++ b/@xen-orchestra/log/capture.js @@ -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) + } +} diff --git a/@xen-orchestra/log/package.json b/@xen-orchestra/log/package.json index 0b9e56b759c..8f60388bd7f 100644 --- a/@xen-orchestra/log/package.json +++ b/@xen-orchestra/log/package.json @@ -4,6 +4,12 @@ "version": "0.6.0", "license": "ISC", "description": "Logging system with decoupled producers/consumer", + "keywords": [ + "async", + "asynchronous", + "capture", + "context" + ], "homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/log", "bugs": "https://github.com/vatesfr/xen-orchestra/issues", "repository": { diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 8a068cfd09e..8b1cff256aa 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -38,6 +38,7 @@ +- @xen-orchestra/log minor - xo-server minor - xo-server-perf-alert minor - xo-server-sdn-controller patch