Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

log

medikoo5.1mISC6.3.2TypeScript support: definitely-typed

Universal pluggable logging utility

log, logger, debug, bunyan, winston

readme

Build status Tests coverage npm version

log

Universal logging utility

Configurable, environment and presentation agnostic, with log levels and namespacing (debug style) support

Usage

1. Write application/library logs

// Default logger (writes at 'info' level)
const log = require("log");

// Log 'info' level message:
log.info("some info message %s", "injected string");

// Get namespaced logger (debug lib style)
const myLibLog = log.get("my-lib");

// Log 'info' level message in context of 'my-lib' namespace:
myLibLog.info("some info message in 'my-lib' namespace context");

// Namespaces can be further nested
const myLibFuncLog = log.get("func");

// Log 'info' level message in context of 'my-lib:func' namespace:
myLibFuncLog.info("some info message in 'my-lib:func' namespace context");

// Log 'error' level message in context of 'my-lib:func' namespace:
myLibFuncLog.error("some error message");

// log output can be dynamically enabled/disabled during runtime
const { restore } = myLibFuncLog.error.disable();
myLibFuncLog.error("error message not really logged");
// Restore previous logs visibiity state
restore();
myLibFuncLog.error("error message to be logged");

2. Initialize log writer in main (starting) process module

e.g. if target is Node.js, then install log-node, and at the top of main module initialize it

require("log-node")();

Available log levels

Mirror of applicable syslog levels (in severity order):

  • debug - debugging information (hidden by default)
  • info - a purely informational message (hidden by default)
  • notice - condition normal, but significant
  • warning (also aliased as warn) - condition warning
  • error - condition error - to notify of errors accompanied with recovery mechanism (hence reported as log and not as uncaught exception)

Note: critical, alert, emergency are not exposed as seem to not serve a use case in context of JS applications, such errors should be exposed as typical exceptions

Output message formatting

log doesn't force any specific arguments handling. Still it is recommended to assume printf-like message format, as all currently available writers are setup to support it. Placeholders support reflects one implemented in Node.js format util

Excerpt from Node.js documentation:

The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument. Supported placeholders are:

  • %s - String.
  • %d - Number (integer or floating point value).
  • %i - Integer.
  • %f - Floating point value.
  • %j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.
  • %o - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, depth: 4, showProxy: true }. This will show the full object including non-enumerable symbols and properties.
  • %O - Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable symbols and properties.
  • %% - single percent sign ('%'). This does not consume an argument.

Note to log writer configuration developers: For cross-env compatibility it is advised to base implementation on sprintf-kit

Enabling log writing

log on its own doesn't write anything to the console or any other means (it just emits events to be consumed by preloaded log writers).

To have logs written, the pre-chosen log writer needs to be initialized in the main (starting) module of a process.

List of available log writers

Note: if some writer is missing, propose a PR

Logs Visibility

Default visibility depends on the enviroment (see chosen log writer for more information), and in most cases is setup through the following environment variables:

LOG_LEVEL

(defaults to notice) Lowest log level from which (upwards) all logs will be exposed.

LOG_DEBUG

Eventual list of namespaces to expose at levels below LOG_LEVEL threshold

List is comma separated as e.g. foo,-foo:bar (expose all foo but not foo:bar).

It follows convention configured within debug. To ease eventual migration from debug, configuration fallbacks to DEBUG env var if LOG_DEBUG is not present.

Timestamps logging

When following env var is set writers are recommended to expose timestamps aside each log message

LOG_TIME
  • rel (default) - Logs time elapsed since logger initialization
  • abs - Logs absolute time in ISO 8601 format

Tests

$ npm test

Project cross-browser compatibility supported by:

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.


Get professional support for d with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

6.3.2 (2024-09-24)

Maintenance Improvements

  • Upgrade prettier-elastic to v3 (acd53a0)

6.3.1 (2021-10-08)

Bug Fixes

  • Ensure emitter is also shared across different instances (3a315ac), closes #11

6.3.0 (2021-10-06)

Features

  • Ensure mutlitple package instances share same master logger (685ec84), closes #11

6.2.0 (2021-09-10)

Features

  • Log: Allow to inject undecorated new lines (140ddfe)

Maintenance Improvements

6.1.0 (2021-09-02)

Features

  • Writer: Allow to customize logger applicability (8d2a4e6)

Maintenance Improvements

6.0.0 (2019-04-10)

Features

  • rename lib/writer to lib/abstract-writer (60388cb)
  • replace registerMaster with getMasterWriter (432b73e)

BREAKING CHANGES

  • lib/register-master was removed in favor of lib/get-master-writer Writer should be registered using 'register' function as returned by get-master-writer
  • Writer base class was moved from lib/writer to lib/abstract-writer

5.1.0 (2019-04-08)

Features

5.0.0 (2019-03-19)

Features

BREAKING CHANGES

  • Level indexes were reversed to match RFC 5424. Now error level has 0 index , and debug 4

4.0.1 (2019-03-18)

4.0.0 (2019-03-18)

Code Refactoring

  • move writer-utils to lib (429aec5)

Features

  • LOG_TIME recommendation and utils (6af9400), closes #9
  • make 'info' level default (4a1fa53)
  • not applicable syslog levels (57c879a)
  • upgrade linter (eba6bb4)

BREAKING CHANGES

  • 'critical', 'alert' and 'emergency' levels were dropped as seem to not serve a use case in context of JS apps
  • Default level was changed from 'debug' to 'info'
  • Or writer-utils were moved to lib

3.2.0 (2018-11-29)

Features

3.1.0 (2018-09-21)

Features

  • support default namespace concept (3c99624)
  • validate namespace token when setting as default (62862c1)

3.0.1 (2018-06-01)

Bug Fixes

  • put default level threshold to notice (e994285)

3.0.0 (2018-06-01)

BREAKING CHANGES

  • Predefined levels are no longer accessible at logger.predefinedLevels property. Instead they should be required from log4/levels
  • For simplicity and to avoid compliance issues, only predefined levels are allowed (it's no longer possible to create custom named levels)
  • enable() and disable() methods no logger return own logger. Instead they return plain object with restore function, that allows to restore to previous state
  • getNs method has been renamed to get
  • ns property has been renamed to namespace
  • nsTokens property has been renamed to namespaceTokens
  • Emitter is not longer accessible at logger.emiter Instead it should be required from log4/emitter
  • getAllLevels method has been renamed to getAllInitializedLevels
  • hasLevel method has been renamed to isLevelInitialized
  • hasNs method has been renamed to isNamespaceInitialized
  • New setupVisibility accepts:

    • levelThreshold - on its basis it's decided logs for which levels are enabled by default

    • debugNamespaceToken - List of namespaces to be exposed for levels below threshold

  • getLevel method was removed. Use direct property names: log.error instead of log.getLevel("error")

  • getAllNs method has been renamed to getAllInitializedNamespaces

Features

  • Default symbols to represent levels (dc2487b)
  • Expose levelIndex on level (aa480ee)
  • Introduce logger.levelRoot property (d1b61c1)
  • isLogger and ensureLogger utils (30d7ab6)
  • registerMaster log writer util (7a36ad8)

2.0.0 (2018-03-22)

Features

  • remove date from log event (7f2903a)
  • remove node env handler (20450ad)
  • remove obsolete event emit (9499b37)

  • feat: emit events also if logger disabled (6eb338d)

BREAKING CHANGES

  • logs for disabled loggers will also be emitted. Handler should check logger.enabled to confirm on whether intetion is to have log visible
  • remove 'event.date` property
  • log:before event was removed
  • Node env handler was moved to outer package, it'll have to be initialized as:

require('log4-node')

1.2.0 (2017-09-22)

Features

  • allow direct setting of isEnabled (1245e21)
  • filter: support nested catch all (93f390c)

1.1.0 (2017-09-07)

Features

  • expose 'warn' alias as predefined property (e8bf927)

1.0.0 (2017-09-07)

Bug Fixes

  • take back exposure of nsChildren (1784c1a)

Features

  • do not force sort on getAllLevels (8489362)
  • ensure emitted messageTokens are array (28bea04)
  • ensure enable/disable return target logger (76228c0)
  • expose array of predefined levels (354db58)
  • expose levels at named properties (91ab1c4)
  • getLevels method (8f36cfe)
  • introduce getAllNs method (e7eabee)
  • introduce hasLevel and hasNs (2887a80)
  • make _children public as nsChildren (0729be7)
  • node.js env logger (2582afe)
  • predefined log levels (ad23c30)
  • replace getLevels with getAllLevels (03478fc)
  • setupEnv functionality (a873045)
  • tolerate whitespace noise in visibility conf (14953be)