mnr-logger v3.0.0
mnr-logger
Opinionated logger for Node.js
You may not need it!
This is a highly opinionated solution aimed at code reuse for a few private projects. You'd be better off using something popular, like winston and friends.
v3 Breaking Changes
From v3,
mnr-loggeris an ESM-only module - you are not able to import it withrequire().Bump minimum supported version of Node.js to v20.
Change type of
metaparameter fromanytounknown.
Archive documentation:
Installation
$ npm install --save mnr-loggerUsage example
import mnrLogger from 'mnr-logger';
const logger = mnrLogger({
appName: 'my-cool-app',
deploymentEnv: 'production'
});
const ERR = new Error('something went wrong');
logger.error(ERR, { transactionId: '12345' });
logger.warn(ERR, { transactionId: '12345' });
logger.info('take a look at foo stuff', { transactionId: '12345' });What It Does
mnr-logger sends log messages to stderr (for error method), or stdout (for warn and info methods).
When process.env.NODE_ENV !== 'production', mnr-logger logs directly to stdout/stderr without any care how many lines it occupies. This is for development mode.
When process.env.NODE_ENV === 'production', mnr-logger creates a POJO with all the pieces of information it has received, then JSON.stringifies it, and sends to stdout/stderr a single line of text. This allows to treat every single line in your logs storage as one logging item and automate processing. After JSON.parsing this string, you'll get a POJO of the following form:
{ISO Date string} timestamp- Timestamp of the moment mnr-logger received the message.{String} appName- Application name that mnr-logger was initialized with.{String} deploymentEnv- Application deployment environment label that mnr-logger was initialized with.{String} level- "error" forerror()method; "warn" forwarn()method; "info" forinfo()method.{String} error- JSON.stringifiedErrorobject that was sent toerror()orwarn()methods. You can JSON.parse it further to get a pretty formatted representation of the error.{Any} meta- Optional. Additional information that was sent to the logger.{String} message- Message passed toinfo()method.
API Reference
mnrLogger(opts: object)
Create a logger instance. You can create as many logger instances as you need in your app.
{String} opts.appName- optional Application name tag. Defaults to an empty string{String} deploymentEnv- optional Application deployment environment tag. Defaults to an empty string
Returns logger instance with error, warn, and info methods.
logger.error(error, [meta])
Logs an error (with some meta data) into stderr.
Parameters:
{Error} error- Error to log{Any} [meta]- optional Any JSON-serializable data.
logger.warn(error, [meta])
Logs an error (with some meta data) into stdout.
Parameters:
{Error} error- Error to log{Any} [meta]- optional Any JSON-serializable data.
logger.info(message, [meta])
Logs an arbitrary text message (with some meta data) into stdout.
Parameters:
{String} message{Any} [meta]- optional Any JSON-serializable data.