Skip to content

lib: implement node:logger - #65840

Open
jasnell wants to merge 1 commit into
nodejs:mainfrom
jasnell:jasnell/node-logger
Open

lib: implement node:logger#65840
jasnell wants to merge 1 commit into
nodejs:mainfrom
jasnell:jasnell/node-logger

Conversation

@jasnell

@jasnell jasnell commented Sep 6, 2026

Copy link
Copy Markdown
Member

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.

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.

The current json output produced by the ConsoleProvider is:

{"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.

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
@jasnell
jasnell requested a review from mcollina September 6, 2026 06:51
@jasnell jasnell added the experimental Issues and PRs related to experimental features. label Sep 6, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Sep 6, 2026
@TheOneTheOnlyJJ

Copy link
Copy Markdown
Contributor

@mertcanaltin, your experience from #60468 may be valuable here.

@codecov

codecov Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.82152% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.19%. Comparing base (7aba44d) to head (f04ec12).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
lib/logger.js 96.81% 13 Missing ⚠️
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     
Files with missing lines Coverage Δ
lib/internal/bootstrap/realm.js 96.98% <100.00%> (+<0.01%) ⬆️
lib/logger.js 96.81% <96.81%> (ø)

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mertcanaltin

Copy link
Copy Markdown
Member

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.

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.

@jasnell

jasnell commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Do custom levels need to be part of the public contract in the first version

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. logger.foo(...)) but the generic logger.log(level, ... gives enough coverage to minimally meet the need.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Issues and PRs related to experimental features. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants