lib: implement node:logger - #65840
Conversation
|
Review requested:
|
|
@mertcanaltin, your experience from #60468 may be valuable here. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65840 +/- ##
==========================================
+ Coverage 90.17% 90.19% +0.01%
==========================================
Files 771 772 +1
Lines 265097 265854 +757
Branches 50362 50559 +197
==========================================
+ Hits 239054 239786 +732
- Misses 17004 17013 +9
- Partials 9039 9055 +16
🚀 New features to boost your workflow:
|
I think this step is very good for review. We started with a big step before, but it was very costly for reviewers. I want to share some topics we had in #60468. I hope they help this PR. Do custom levels need to be part of the public contract in the first version? I'm not sure. Level names and numeric ordering are a long-term cost for both users and providers. My suggestion is to limit the first version to the built-in levels, so the API stays smaller. Providers can do their own mapping internally when necessary. This comment is not a blocker for this PR. |
I think they ought to be, yes, at least to an extent. I absolutely don't think we should do the automatic custom level method installation like what pino does (e.g. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
1a8be55 to
b1e0970
Compare
b1f2cc0 to
10db06e
Compare
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
10db06e to
efff647
Compare
A simpler attempt to add a structured logging API.
Uses a provider model similar to VFS.
This implements four providers out of the box:
ConsoleProvider-- emits to stderr or stdout usingUtf8StreamEventProvider-- implements EventEmitter, emits logs as eventsDiagnosticsProvider-- emits to diagnostics channelsAggregateProvider-- fans out to multiple providersThe
Loggeritself 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.The current json output produced by the
ConsoleProvideris:{"attributes":{},"bindings":{},"level":{"name":"info","value":30},"message":"test","name":"foo","timestamp":1788677600214,"pid":670179}But there are options to produce a "flattened" version
We should bikeshed the format a bit before this lands.
Updated... I've tweaked things just a bit more.
The module itself exports the create function directly, making it possible to do:
It still has all the exports off of it.
There's also a singleton default provider. When
create()is called without a provider option, then the default singletonConsoleProvideris created lazily. An application can override the default usingsetDefaultProvider...This way, modules can just create a logger and dispatch to the application's default configured provider without worrying about where those will go.
Also includes a fix to
fs.Utf8Stream's async and sync flush handling that I noticed wasn't working quite right when testing.