Universal logging utility
Configurable, environment and presentation agnostic, with log levels and namespacing (debug style) support
// Default logger writes at 'info' levelconst log = ;// Log 'info' level message:;// Get namespaced logger (debug lib style)log = log;// Log 'info' level message in context of 'my-lib' namespace:;// Namespaces can be nestedlog = log;// Log 'info' level message in context of 'my-lib:func' namespace:;// Log 'error' level message in context of 'my-lib:func' namespace:log;// log output can be dynamically enabled/disabled during runtimeconst restore = logerror;log;// Restore previous logs visibiity state;log;
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)
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:
%d- Number (integer or floating point value).
%f- Floating point value.
%j- JSON. Replaced with the string '[Circular]' if the argument contains circular references.
%%- 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
Default visibility depends on the enviroment (see chosen log writer for more information), and in most cases is setup through the following environment variables:
notice) Lowest log level from which (upwards) all logs will be exposed.
Eventual list of namespaces to expose at levels below
List is comma separated as e.g.
foo,-foo:bar (expose all
foo but not
Writers are recommended to to expose timestamps aside each log when following env var is set
rel(default) - Logs time elapsed since logger initialization
abs- Logs absolute time in ISO 8601 format
$ npm test
Project cross-browser compatibility supported by: