lib: implement node:logger - #65840
Conversation
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
|
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.20% 90.19% -0.01%
==========================================
Files 771 772 +1
Lines 264622 265031 +409
Branches 50228 50304 +76
==========================================
+ Hits 238704 239051 +347
- Misses 16907 16971 +64
+ Partials 9011 9009 -2
🚀 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. |
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.
The
Loggerkeeps 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}We should bikeshed the format a bit before this lands.