GeezerVison is showing github.com — lib: implement node:logger · nodejs/node@71d82d2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// End-to-end cost of logging through ConsoleProvider with the default JSON | | 4 | +// serializer. The provider's stream write() is replaced to avoid I/O latency. | | 5 | + | | 6 | +const common = require('../common.js'); | | 7 | + | | 8 | +const bench = common.createBenchmark(main, { | | 9 | + n: [5e5], | | 10 | + variant: ['json', 'flatten-pid', 'error'], | | 11 | +}, { | | 12 | + flags: ['--experimental-logger', '--no-warnings'], | | 13 | +}); | | 14 | + | | 15 | +function main({ n, variant }) { | | 16 | + const { ConsoleProvider, create } = require('node:logger'); | | 17 | + | | 18 | + const provider = new ConsoleProvider({ | | 19 | + flatten: variant === 'flatten-pid', | | 20 | + pid: variant === 'flatten-pid', | | 21 | + }); | | 22 | + let bytes = 0; | | 23 | + provider.stream.write = (data) => { | | 24 | + bytes += data.length; | | 25 | + return true; | | 26 | + }; | | 27 | + const logger = create(provider, { | | 28 | + name: 'bench', | | 29 | + bindings: { service: 'api' }, | | 30 | + }); | | 31 | + | | 32 | + if (variant === 'error') { | | 33 | + const err = new Error('boom'); | | 34 | + err.code = 'E_BOOM'; | | 35 | + bench.start(); | | 36 | + for (let i = 0; i < n; i++) logger.error('failed', { err }); | | 37 | + bench.end(n); | | 38 | + } else { | | 39 | + bench.start(); | | 40 | + for (let i = 0; i < n; i++) logger.info('hello', { i, user: 'x' }); | | 41 | + bench.end(n); | | 42 | + } | | 43 | + | | 44 | + if (bytes === 0) throw new Error('nothing was serialized'); | | 45 | +} |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Cost of creating loggers and child loggers. | | 4 | + | | 5 | +const common = require('../common.js'); | | 6 | + | | 7 | +const bench = common.createBenchmark(main, { | | 8 | + n: [1e6], | | 9 | + type: ['create', 'child'], | | 10 | +}, { | | 11 | + flags: ['--experimental-logger', '--no-warnings'], | | 12 | +}); | | 13 | + | | 14 | +function main({ n, type }) { | | 15 | + const { ConsoleProvider, create } = require('node:logger'); | | 16 | + | | 17 | + const provider = new ConsoleProvider(); | | 18 | + const parent = create(provider, { name: 'bench' }); | | 19 | + const loggers = new Array(1024); | | 20 | + | | 21 | + if (type === 'child') { | | 22 | + bench.start(); | | 23 | + for (let i = 0; i < n; i++) { | | 24 | + loggers[i & 1023] = parent.child({ requestId: i }); | | 25 | + } | | 26 | + bench.end(n); | | 27 | + } else { | | 28 | + bench.start(); | | 29 | + for (let i = 0; i < n; i++) { | | 30 | + loggers[i & 1023] = create(provider, { name: 'bench' }); | | 31 | + } | | 32 | + bench.end(n); | | 33 | + } | | 34 | +} |
Collapse file Copy file name to clipboard +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Calls to level methods for a level the provider has disabled. | | 4 | + | | 5 | +const common = require('../common.js'); | | 6 | + | | 7 | +const bench = common.createBenchmark(main, { | | 8 | + n: [1e8], | | 9 | + provider: ['console', 'custom'], | | 10 | + attributes: ['none', 'object'], | | 11 | + child: [0, 1], | | 12 | +}, { | | 13 | + flags: ['--experimental-logger', '--no-warnings'], | | 14 | +}); | | 15 | + | | 16 | +function main({ n, provider, attributes, child }) { | | 17 | + const { ConsoleProvider, create } = require('node:logger'); | | 18 | + | | 19 | + let logger = create(provider === 'console' ? | | 20 | + new ConsoleProvider({ level: 'info' }) : | | 21 | + { | | 22 | + isEnabled(level) { return level.value >= 30; }, | | 23 | + log() {}, | | 24 | + }); | | 25 | + if (child) logger = logger.child({ requestId: 1 }); | | 26 | + | | 27 | + if (attributes === 'object') { | | 28 | + bench.start(); | | 29 | + for (let i = 0; i < n; i++) logger.debug('hello', { i }); | | 30 | + bench.end(n); | | 31 | + } else { | | 32 | + bench.start(); | | 33 | + for (let i = 0; i < n; i++) logger.debug('hello'); | | 34 | + bench.end(n); | | 35 | + } | | 36 | +} |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Cost of composing an enabled log event, with a provider that does no work. | | 4 | + | | 5 | +const common = require('../common.js'); | | 6 | + | | 7 | +const bench = common.createBenchmark(main, { | | 8 | + n: [1e6], | | 9 | + provider: ['minimal', 'custom'], | | 10 | + variant: ['no-attributes', 'attributes', 'function-binding'], | | 11 | +}, { | | 12 | + flags: ['--experimental-logger', '--no-warnings'], | | 13 | +}); | | 14 | + | | 15 | +function main({ n, provider, variant }) { | | 16 | + const { create } = require('node:logger'); | | 17 | + const { AsyncLocalStorage } = require('node:async_hooks'); | | 18 | + | | 19 | + let last; | | 20 | + const log = (event) => { last = event; }; | | 21 | + const als = new AsyncLocalStorage(); | | 22 | + const bindings = variant === 'function-binding' ? | | 23 | + { service: 'api', request: () => als.getStore() } : | | 24 | + { service: 'api' }; | | 25 | + const logger = create(provider === 'minimal' ? { log } : { | | 26 | + isEnabled(level) { return level.value >= 30; }, | | 27 | + log, | | 28 | + }, { name: 'bench', bindings }); | | 29 | + | | 30 | + switch (variant) { | | 31 | + case 'no-attributes': | | 32 | + bench.start(); | | 33 | + for (let i = 0; i < n; i++) logger.info('hello'); | | 34 | + bench.end(n); | | 35 | + break; | | 36 | + case 'attributes': | | 37 | + bench.start(); | | 38 | + for (let i = 0; i < n; i++) logger.info('hello', { i, user: 'x' }); | | 39 | + bench.end(n); | | 40 | + break; | | 41 | + case 'function-binding': | | 42 | + als.enterWith(1); | | 43 | + bench.start(); | | 44 | + for (let i = 0; i < n; i++) logger.info('hello', { i }); | | 45 | + bench.end(n); | | 46 | + break; | | 47 | + } | | 48 | + | | 49 | + if (last === undefined) throw new Error('no event was logged'); | | 50 | +} |
Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +12Lines changed: 12 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1545,6 +1545,16 @@ Specify the `module` containing exported [asynchronous module customization hook | 1545 | 1545 |
| 1546 | 1546 | This feature requires `--allow-worker` if used with the [Permission Model][]. | 1547 | 1547 |
| | 1548 | +### `--experimental-logger` | | 1549 | + | | 1550 | +<!-- YAML | | 1551 | +added: REPLACEME | | 1552 | +--> | | 1553 | + | | 1554 | +> Stability: 1.1 - Active development | | 1555 | +
| | 1556 | +Enable the experimental [`node:logger`][] module. | | 1557 | + | 1548 | 1558 | ### `--experimental-network-inspection` | 1549 | 1559 |
| 1550 | 1560 | <!-- YAML | @@ -4276,6 +4286,7 @@ one is included in the list below. | 4276 | 4286 | * `--experimental-import-text` | 4277 | 4287 | * `--experimental-json-modules` | 4278 | 4288 | * `--experimental-loader` | | 4289 | +* `--experimental-logger` | 4279 | 4290 | * `--experimental-modules` | 4280 | 4291 | * `--experimental-package-map` | 4281 | 4292 | * `--experimental-print-required-tla` | @@ -4961,6 +4972,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 | 4961 | 4972 | [`inspector.open()`]: inspector.md#inspectoropenport-host-wait | 4962 | 4973 | [`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout | 4963 | 4974 | [`node:ffi`]: ffi.md | | 4975 | +[`node:logger`]: logger.md | 4964 | 4976 | [`node:sqlite`]: sqlite.md | 4965 | 4977 | [`node:stream/iter`]: stream_iter.md | 4966 | 4978 | [`node:vfs`]: vfs.md | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +4-3Lines changed: 4 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -8563,12 +8563,13 @@ Close the stream gracefully, flushing the internal buffer before closing. | 8563 | 8563 | * `callback` {Function} | 8564 | 8564 | * `err` {Error|null} An error if the flush failed, otherwise `null`. | 8565 | 8565 |
| 8566 | | -Writes the current buffer to the file if a write was not in progress. Do | 8567 | | -nothing if `minLength` is zero or if it is already writing. | | 8566 | +Writes the current buffer to the file. The callback is invoked after pending | | 8567 | +writes complete. | 8568 | 8568 |
| 8569 | 8569 | #### `utf8Stream.flushSync()` | 8570 | 8570 |
| 8571 | | -Flushes the buffered data synchronously. This is a costly operation. | | 8571 | +Flushes the buffered data synchronously. This is a costly operation. An | | 8572 | +`ERR_INVALID_STATE` error is thrown if the stream is currently writing. | 8572 | 8573 |
| 8573 | 8574 | #### `utf8Stream.fsync` | 8574 | 8575 |
| |
Collapse file Copy file name to clipboardExpand all lines: doc/api/index.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | * [Inspector](inspector.md) | 37 | 37 | * [Internationalization](intl.md) | 38 | 38 | * [Iterable Streams API](stream_iter.md) | | 39 | +* [Logger](logger.md) | 39 | 40 | * [Modules: CommonJS modules](modules.md) | 40 | 41 | * [Modules: ECMAScript modules](esm.md) | 41 | 42 | * [Modules: `node:module` API](module.md) | |
|