mnr-logger v2.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.
v2 Breaking Changes
Bump minimum supported version of Node.js to v16.
Remove test stubs and hence, remove
sinon
dependency. Practice has shown this functionality is not used.
Archive documentation: v1.
Installation
$ npm install --save mnr-logger
Usage example
const mnrLogger = require('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.stringifiedError
object that was sent toerror()
orwarn()
methods. You can JSON.parse it further to get a pretty formatted representation of the error.{Any} meta
- Additional information that was sent to the logger (which defaults to an empty object).{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]
- Any JSON-serializable data. Defaults to an empty object.
logger.warn(error, [meta])
Logs an error (with some meta data) into stdout.
Parameters:
{Error} error
- Error to log{Any} [meta]
- Any JSON-serializable data. Defaults to an empty object.
logger.info(message, [meta])
Logs an arbitrary text message (with some meta data) into stdout.
Parameters:
{String} message
{Any} [meta]
- Any JSON-serializable data. Defaults to an empty object.