simple-log-node
v1.0.2
Published
A simple logger for Node.js application provides JSON formatted logging and supports masking of PII (Personally Identifiable Information) data in logging.
Downloads
31
Readme
simple-log-node Logger
A simple logger for Node.js application provides JSON formatted logging and supports masking of PII (Personally Identifiable Information) data in logging.
Installation
npm install simple-log-node
Usage
// require
const Logger = require('simple-log-node);
// instantiation
// logging context
const context = {
name: <string>, // required
application: <string>, //optional
category: <string>, // optional
type: <string>, // optional default to 'application'
log_id: <any>, // optional
time: <timestamp> // optional
};
// logging level
const logLevel = 'info' or 'debug'; // defaults to info
//mask options for masking PII fields in logs output
const maskOptions = {
maskFileds: <object_array> // default 'password'
}
// create logger
const logger = new Logger(
context, // required
logLevel, // optional
maskOptions // optional
);
// logging
logger.info('Something works fine!');
//=> {"name":"Sample File","level":30,"category":"INFO","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something works fine!","time":"2020-06-20T17:43:14.951Z","v":0}
logger.error('Something went wrong!');
//=> {"name":"Sample File","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something went wrong!","time":"2020-06-20T17:43:14.955Z","v":0}
logger.fatal('Something broken!');
//=> {"name":"Sample File","level":60,"category":"FATAL","environment":"local","type":"application","log_id":"79afb8aa-c37f-41ff-b803-32ec98003403","data_object":{},"msg":"Something broken!","time":"2020-06-20T17:43:14.956Z","v":0}
logger.debug('Something to debug!');
//=> {"name":"Sample File","level":20,"category":"DEBUG","environment":"local","type":"application","log_id":"97019e69-f7e6-4522-813f-9477e5afd67a","data_object":{},"msg":"Something to debug!","time":"2020-06-20T17:45:49.735Z","v":0}
Examples
1. General Logging
// user.js
const Logger = require('simple-log-node');
const logger = new Logger( { name: 'User Class' } );
// let's create a sample user object
const user = {
id: 100,
name: 'John Doe',
email: '[email protected]',
mobile: '7777777777'
};
// logging with data
logger.info('User created successfully', user);
//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"ae5163e2-c680-4d6b-b380-fc336ba8873d","data_object":{"id":100,"name":"John Doe","email":"[email protected]","mobile":"7777777777"},"msg":"User created successfully","time":"2020-06-20T18:13:58.875Z","v":0}
// logging with extra metadata
const someMetadata = {
RequestId: "7823djbhbcj732"
};
logger.info('User created successfully', user, someMetadata);
//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"1bad0c33-b926-4d14-80cf-57e7d1fd1827","RequestId":"7823djbhbcj732","data_object":{"id":100,"name":"John Doe","email":"[email protected]","mobile":"7777777777"},"msg":"User created successfully","time":"2020-06-20T18:20:10.820Z","v":0}
2. Logging with masking PII data
// user.js
const Logger = require('simple-log-node');
// configure masking options
const maskOptions = {
maskFields: ['email', 'mobile']
};
const logger = new Logger( { name: 'User Class' },'info', maskOptions);
// let's create a sample user object
const user = {
id: 100,
name: 'John Doe',
email: '[email protected]',
mobile: '7777777777'
};
//logging message with masking `email` and `mobile`
logger.info(`User - ${user.id} created successfully`, user);
//=> {"name":"User Class","level":30,"category":"INFO","environment":"local","type":"application","log_id":"152428e7-4f2c-4597-b782-efdceee48f83","data_object":{"id":100,"name":"John Doe","email":"*******","mobile":"*******"},"msg":"User - 100 created successfully","time":"2020-06-20T18:34:57.290Z","v":0}
3. Logging Error Messages
// sample.js
const Logger = require('simple-log-node');
const logger = new Logger( { name: 'User Class' },'info');
// let's thrown an error
try {
throw new TypeError("Something went wrong");
} catch (err) {
// simple error message
logger.error(`Error : ${err.message}`);
//=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","data_object":{},"msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.330Z","v":0}
// error message with stack trace
logger.error(`Error : ${err.message}`, err.stack);
//=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","data_object":"TypeError: Something went wrong\n at Object.<anonymous> (C:\\my-workspace\\simple-log-node\\test\\logger.test.js:6:11)\n at Module._compile (internal/modules/cjs/loader.js:776:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)\n at Module.load(internal/modules/cjs/loader.js:653:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:593:12)\n at Function.Module._load (internal/modules/cjs/loader.js:585:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.334Z","v":0}
// error message with extra metadata
logger.error(`Error : ${err.message}`, err.stack, {errorClass : err.name});
//=> {"name":"User Class","level":50,"category":"ERROR","environment":"local","type":"application","log_id":"fdf268e8-9971-4b44-ab2f-446ebbff424a","errorClass":"TypeError","data_object":"TypeError: Something went wrong\n at Object.<anonymous> (C:\\my-workspace\\simple-log-node\\test\\logger.test.js:6:11)\n at Module._compile (internal/modules/cjs/loader.js:776:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)\n at Module.load (internal/modules/cjs/loader.js:653:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:593:12)\n at Function.Module._load (internal/modules/cjs/loader.js:585:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","msg":"Error : Something went wrong","time":"2020-06-20T18:54:56.334Z","v":0}
}