![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
This is a considerable departure from the traditional logger.
This logger is meant for use in third party modules and packages. It's so that modules can have a very simple way to offer optional visibility in to the modules inner workings.
To this end, it does not have log levels like info, warn, etc. It simply allows the creation of a logger for your module (with a name) and offer a single function to take messages and a context for those messages.
This module is both an implementation and a light specification. Loggers can be quite opinionated and the creators of modules are unlikely to solidify around a single implementation of anything.
A logging library should be a single function that returns logger functions.
var logging = require('logging')
, log = logging('mymodulename')
;
When running an application in a production environment, logging is the one of the few things you need to be global that is outside of node core.
Users of this specification should require the user to set the process.logging to the desired module. A library must never set this property automatically as it will override any other logger the user may want to use.
server.js
process.logging = require('logging')
var request = require('request')
Notice that this line comes before loading any other module. This is important, modules will likely check for process.logging as soon as they are required.
Using the logger is quite easy within a module.
if (process.logging) var log = process.logging('mymodule')
else var log = function () {}
...
module.exports = fancy (options) {
log('new fancy %method for %url', options)
}
...
fancy({method:'GET', url:'http://www.google.com'})
The final piece of this spec is visible in the last example.
Log functions take two arguments, a message and a context.
Named string interpolation from the context should be supported. This prevents many errors related to using the + operator in building the message string.
The rest of this documentation covers features which should not be considered part of the specification.
Enabling stdout or stderr printing of logs.
require('logging').stdout()
require('logging').stderr()
All will print [mymodule] new fancy GET for http://www.google.com
.
FAQs
Lightweight informative modern console logging.
The npm package logging receives a total of 3,274 weekly downloads. As such, logging popularity was classified as popular.
We found that logging demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.