Universal Pluggable Logging Utility
The `log` package provides a universal and pluggable logging utility designed for JavaScript applications, currently stable at version 6.3.2. It focuses on being configurable, environment, and presentation agnostic, differentiating itself by not handling output directly but emitting events for external 'writers' (e.g., `log-node` for Node.js environments). Releases are made as needed, with several maintenance and minor feature updates occurring within the last few years. Key features include syslog-compatible log levels (debug, info, notice, warning, error), `debug`-style namespacing for granular control, and support for printf-like message formatting with various placeholders (e.g., `%s`, `%d`, `%j`, `%o`). This design allows developers to write application logs once and integrate different output mechanisms without changing core logging logic. It does not expose `critical`, `alert`, or `emergency` levels, expecting these to be handled as typical exceptions. A crucial differentiator is its modular approach, requiring an explicit 'writer' package to actually emit log messages, making it highly adaptable to various runtime environments and output formats.
Common errors
-
No log messages appear in the console/output.
cause The `log` package requires a separate 'writer' module to process and output log events. It does not write to the console by default.fixInstall and initialize a writer package, such as `log-node`, at the entry point of your application. Example: `npm install log-node` then `import 'log-node';` or `require('log-node')();`. -
My 'error' messages are being treated as low severity, or 'debug' messages are high severity.
cause In version 5.0.0, the numerical indexing of log levels was reversed to align with RFC 5424. `error` is now 0 (highest severity), and `debug` is 4 (lowest severity).fixEnsure your code, especially any custom writers or filters, is aware of the reversed level indexing if you are comparing levels by their numerical values. It's safer to use the named methods (e.g., `log.error()`, `log.debug()`) rather than relying on index values.
Warnings
- breaking The internal API for registering and accessing the master writer changed in v6.0.0. `lib/register-master` was removed in favor of `lib/get-master-writer`, and `lib/writer` was renamed to `lib/abstract-writer`.
- breaking In v5.0.0, the internal level indexes were reversed to align with RFC 5424 syslog severity order. `error` now has index `0`, and `debug` has `4`. This means custom logic or writers that relied on the numerical index of levels will need to be updated.
- gotcha The `log` package itself does not produce any output. It functions as an event emitter. To see logs in your console or other destinations, you *must* install and initialize a separate 'writer' package, such as `log-node` for Node.js environments.
- gotcha The `log` package intentionally does not expose `critical`, `alert`, or `emergency` syslog levels. These are deemed suitable for handling as typical JavaScript exceptions rather than log messages within application code.
Install
-
npm install log -
yarn add log -
pnpm add log
Imports
- log
const log = require('log');import log from 'log';
- log.get
import { get } from 'log'; // Incorrectly assumes `get` is a named exportconst namespaceLog = log.get('my-namespace'); - log-node initialization
require('log-node'); // In ESM context, this should be an import statementimport 'log-node';
Quickstart
import log from 'log';
import 'log-node'; // Initialize the Node.js writer for log output
// Default logger (writes at 'info' level by default)
log.info("Application started: %s", new Date().toISOString());
// Get a namespaced logger (similar to 'debug' library style)
const myLibLog = log.get('my-lib');
myLibLog.info("Initializing 'my-lib' module with config: %j", { debug: true, port: 3000 });
// Namespaces can be nested
const myLibFuncLog = myLibLog.get('func');
myLibFuncLog.debug("Executing critical function 'processData' for ID: %d", 12345);
// Log an 'error' level message
myLibFuncLog.error("Failed to process data due to an unexpected error. Details: %s", "Invalid input");
// Dynamically disable/enable logging for a specific level
const { restore } = myLibFuncLog.debug.disable();
myLibFuncLog.debug("This debug message should not be logged.");
restore(); // Restore previous visibility state
myLibFuncLog.debug("This debug message should now be logged.");