Skip to content

Commit 7865682

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 5e3bbc4 commit 7865682

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
@@ -1545,6 +1545,16 @@ Specify the `module` containing exported [asynchronous module customization hook
15451545

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

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+
15481558
### `--experimental-network-inspection`
15491559

15501560
<!-- YAML
@@ -4276,6 +4286,7 @@ one is included in the list below.
42764286
* `--experimental-import-text`
42774287
* `--experimental-json-modules`
42784288
* `--experimental-loader`
4289+
* `--experimental-logger`
42794290
* `--experimental-modules`
42804291
* `--experimental-package-map`
42814292
* `--experimental-print-required-tla`
@@ -4961,6 +4972,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
49614972
[`inspector.open()`]: inspector.md#inspectoropenport-host-wait
49624973
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
49634974
[`node:ffi`]: ffi.md
4975+
[`node:logger`]: logger.md
49644976
[`node:sqlite`]: sqlite.md
49654977
[`node:stream/iter`]: stream_iter.md
49664978
[`node:vfs`]: vfs.md

‎doc/api/fs.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,12 +8563,13 @@ Close the stream gracefully, flushing the internal buffer before closing.
85638563
* `callback` {Function}
85648564
* `err` {Error|null} An error if the flush failed, otherwise `null`.
85658565
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.
85688568
85698569
#### `utf8Stream.flushSync()`
85708570
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.
85728573
85738574
#### `utf8Stream.fsync`
85748575

‎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)