Opinionated logger for Node.js
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.
-
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.
$ npm install --save mnr-logger
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' });
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.
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.
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.
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.
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.