Skip to content

Commit bee5d56

Browse files
committed
lib: implement node:logger
A simpler attempt to add a structured logging API. Uses a provider model similar to VFS. This implements two providers out of the box, ConsoleProvider and ReadableProvider. ConsoleProvider is the default and uses Utf8Stream to emit to either stdout or stderr. ```js const { create } = require('node:logger'); const logger = create(); // default logger to console logger.info('foo'); // ... const als = new AsyncLocalStorage(); const logger2 = create(new ConsoleProvider({ pid: true }), { name: 'foo', bindings: { 'abc': 'included in every log line', 'xyz': als, // current als.getStore() included in // every log line } }); logger2.info('foo', { baz: 1 }); ``` The `Logger` keeps things as simple as possible, leaving actual handling of the log events to the providers, which can be fully customized. Easy to adapt to other loggers like pino or extend capabilities without directly touching the facade. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode
1 parent 577b9c2 commit bee5d56

22 files changed

Lines changed: 2500 additions & 43 deletions

doc/api/cli.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,16 @@ Specify the `module` containing exported [asynchronous module customization hook
14951495

14961496
This feature requires `--allow-worker` if used with the [Permission Model][].
14971497

1498+
### `--experimental-logger`
1499+
1500+
<!-- YAML
1501+
added: REPLACEME
1502+
-->
1503+
1504+
> Stability: 1.1 - Active development
1505+
1506+
Enable the experimental [`node:logger`][] module.
1507+
14981508
### `--experimental-network-inspection`
14991509

15001510
<!-- YAML
@@ -4097,6 +4107,7 @@ one is included in the list below.
40974107
* `--experimental-import-text`
40984108
* `--experimental-json-modules`
40994109
* `--experimental-loader`
4110+
* `--experimental-logger`
41004111
* `--experimental-modules`
41014112
* `--experimental-package-map`
41024113
* `--experimental-print-required-tla`
@@ -4769,6 +4780,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
47694780
[`import` specifier]: esm.md#import-specifiers
47704781
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
47714782
[`node:ffi`]: ffi.md
4783+
[`node:logger`]: logger.md
47724784
[`node:sqlite`]: sqlite.md
47734785
[`node:stream/iter`]: stream_iter.md
47744786
[`node:vfs`]: vfs.md

doc/api/fs.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8549,12 +8549,13 @@ Close the stream gracefully, flushing the internal buffer before closing.
85498549
* `callback` {Function}
85508550
* `err` {Error|null} An error if the flush failed, otherwise `null`.
85518551
8552-
Writes the current buffer to the file if a write was not in progress. Do
8553-
nothing if `minLength` is zero or if it is already writing.
8552+
Writes the current buffer to the file. The callback is invoked after pending
8553+
writes complete.
85548554
85558555
#### `utf8Stream.flushSync()`
85568556
8557-
Flushes the buffered data synchronously. This is a costly operation.
8557+
Flushes the buffered data synchronously. This is a costly operation. An
8558+
`ERR_INVALID_STATE` error is thrown if the stream is currently writing.
85588559
85598560
#### `utf8Stream.fsync`
85608561

doc/api/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* [Inspector](inspector.md)
3737
* [Internationalization](intl.md)
3838
* [Iterable Streams API](stream_iter.md)
39+
* [Logger](logger.md)
3940
* [Modules: CommonJS modules](modules.md)
4041
* [Modules: ECMAScript modules](esm.md)
4142
* [Modules: `node:module` API](module.md)

0 commit comments

Comments
 (0)