dynamic-express-logging
Advanced tools
Comparing version 1.0.1 to 1.0.2
39
index.js
@@ -5,3 +5,3 @@ /** | ||
var logger; | ||
var customLogger; | ||
var util = require('util'); | ||
@@ -11,4 +11,4 @@ var maskingObject; | ||
module.exports.init = function (log, jsonMaskingObject) { | ||
logger = log; | ||
module.exports.init = function (logger, jsonMaskingObject) { | ||
customLogger = logger; | ||
maskingObject = jsonMaskingObject; | ||
@@ -18,6 +18,11 @@ }; | ||
module.exports.requestMiddleware = function (req, res, next) { | ||
// logging incoming request | ||
var incomingRequest = _.cloneDeep(req); | ||
if (isNeededMasking(req)) maskRequest(incomingRequest.body); | ||
logger.info(util.format("Start handling request %s:%s", incomingRequest.method, incomingRequest.originalUrl), {headers: JSON.stringify(secureHeaders(req, incomingRequest.headers))}, {body: JSON.stringify(incomingRequest.body)}); | ||
var maskObject = getMaskObject(req); | ||
if (maskObject) maskBody(incomingRequest.body); | ||
customLogger.info(util.format("Start handling request %s:%s", incomingRequest.method, incomingRequest.originalUrl), {headers: JSON.stringify(secureHeaders(maskObject, req, incomingRequest.headers))}, {body: JSON.stringify(incomingRequest.body)}); | ||
// logging response | ||
var end = res.end; | ||
@@ -28,4 +33,5 @@ res.end = function (chunk, encoding) { | ||
var responseBody = safeJSONParse(chunk); | ||
if (isNeededMasking(req) && res.statusCode == 200) maskRequest(responseBody); | ||
logger.info("Finish handle %s:%s request", req.method, req.originalUrl, util.format("statusCode: %s", res.statusCode), util.format("headers: %j", req.headers), util.format("responseBody: %j", responseBody)); | ||
var maskObject = getMaskObject(req); | ||
if (maskObject && res.statusCode == 200) maskBody(responseBody); | ||
customLogger.info("Finish handle %s:%s request", req.method, req.originalUrl, util.format("statusCode: %s", res.statusCode), util.format("headers: %j", secureHeaders(maskObject, req, req.headers)), util.format("responseBody: %j", responseBody)); | ||
}; | ||
@@ -45,18 +51,17 @@ | ||
var isNeededMasking = function (req) { | ||
return maskingObject.filter(function (request) { | ||
var getMaskObject = function (req) { | ||
var maskObjects = maskingObject.filter(function (request) { | ||
if (req.originalUrl.indexOf(request.endPoint) > -1 && req.method === request.method) return true; | ||
else return false; | ||
}); | ||
if (maskObjects && maskObjects.length > 0) return maskObjects[0]; | ||
else return undefined; | ||
}; | ||
var secureHeaders = function (req, headers) { | ||
var secureHeaders = function (maskObject, req, headers) { | ||
var incomingHeader = _.cloneDeep(headers); | ||
var maskObject = maskingObject.filter(function (request) { | ||
if (req.originalUrl.indexOf(request.endPoint) > -1 && req.method === request.method) return true; | ||
else return false; | ||
}); | ||
if (maskObject) { | ||
maskObject.execludeHeaders.forEach(function (headerToBeRemoved) { | ||
if (maskObject && maskObject.length > 0) { | ||
maskObject[0].excludeHeaders.forEach(function (headerToBeRemoved) { | ||
delete incomingHeader[headerToBeRemoved]; | ||
@@ -70,3 +75,3 @@ }) | ||
var maskRequest = function (body) { | ||
var maskBody = function (body) { | ||
if (body) { | ||
@@ -73,0 +78,0 @@ var keys = Object.keys(body); |
{ | ||
"name": "dynamic-express-logging", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "express logging for incoming request and response", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3301
67