From c293b947f2f199ec15900a9c41ddc40c9e360b3d Mon Sep 17 00:00:00 2001 From: zetxx Date: Thu, 14 May 2020 18:09:23 +0300 Subject: [PATCH 1/4] feat: logging ability --- index.js | 1 + middleware/index.js | 4 ++++ middleware/logger/index.js | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 middleware/logger/index.js diff --git a/index.js b/index.js index 395f41e..ddc26f0 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ module.exports = ({utPort, registerErrors}) => { bodyParser: {}, basicAuth: false, jwt: false, + logger: false, router: {}, auth: {}, validator: { diff --git a/middleware/index.js b/middleware/index.js index 2f751a6..29478e1 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -15,6 +15,10 @@ module.exports = [ // middleware order name: 'swaggerUI', factory: require('./swaggerUI') }, + { + name: 'logger', + factory: require('./logger') + }, { name: 'cors', factory: require('./cors') diff --git a/middleware/logger/index.js b/middleware/logger/index.js new file mode 100644 index 0000000..b34db59 --- /dev/null +++ b/middleware/logger/index.js @@ -0,0 +1,18 @@ +const getDataByPath = (path = [], obj = {}) => { + const cp = path.concat([]); + const curr = cp.shift(); + if (cp.length === 0) { + return obj[curr]; + } else { + return getDataByPath(cp, obj[curr]); + } +}; +module.exports = ({port, options: {logPaths = []}}) => { + return (ctx, next) => { + logPaths.map((path) => { + const data = getDataByPath(path, ctx) + data && port.log.info({path, data}); + }); + next(); + }; +}; From 3289ae52f6fc83ea26f7eb86fb16784d488450e2 Mon Sep 17 00:00:00 2001 From: zetxx Date: Thu, 14 May 2020 18:11:57 +0300 Subject: [PATCH 2/4] fix: lint --- middleware/logger/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/logger/index.js b/middleware/logger/index.js index b34db59..f1a5a0f 100644 --- a/middleware/logger/index.js +++ b/middleware/logger/index.js @@ -10,7 +10,7 @@ const getDataByPath = (path = [], obj = {}) => { module.exports = ({port, options: {logPaths = []}}) => { return (ctx, next) => { logPaths.map((path) => { - const data = getDataByPath(path, ctx) + const data = getDataByPath(path, ctx); data && port.log.info({path, data}); }); next(); From 65a3adfed6a9b853b019d5df7f19d5abd3ddef69 Mon Sep 17 00:00:00 2001 From: zetxx Date: Thu, 14 May 2020 21:51:19 +0300 Subject: [PATCH 3/4] fix: ordering --- middleware/index.js | 16 ++++++++-------- middleware/logger/index.js | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/middleware/index.js b/middleware/index.js index 29478e1..cac83a2 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -15,6 +15,14 @@ module.exports = [ // middleware order name: 'swaggerUI', factory: require('./swaggerUI') }, + { + name: 'formParser', + factory: require('./formParser') + }, + { + name: 'bodyParser', + factory: require('./bodyParser') + }, { name: 'logger', factory: require('./logger') @@ -31,14 +39,6 @@ module.exports = [ // middleware order name: 'etag', factory: require('./etag') }, - { - name: 'formParser', - factory: require('./formParser') - }, - { - name: 'bodyParser', - factory: require('./bodyParser') - }, { name: 'router', factory: require('./router') diff --git a/middleware/logger/index.js b/middleware/logger/index.js index f1a5a0f..745283a 100644 --- a/middleware/logger/index.js +++ b/middleware/logger/index.js @@ -9,10 +9,10 @@ const getDataByPath = (path = [], obj = {}) => { }; module.exports = ({port, options: {logPaths = []}}) => { return (ctx, next) => { - logPaths.map((path) => { - const data = getDataByPath(path, ctx); - data && port.log.info({path, data}); - }); + const data = logPaths + .map((path) => getDataByPath(path, ctx)) + .filter(Boolean); + data && port.log.info({logPaths, data}); next(); }; }; From 79120bd8c27e2cbeb5345ad439d0217971e0c6e8 Mon Sep 17 00:00:00 2001 From: zetxx Date: Fri, 15 May 2020 14:31:53 +0300 Subject: [PATCH 4/4] fix: ordering --- middleware/index.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/middleware/index.js b/middleware/index.js index cac83a2..e2f053b 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -15,18 +15,6 @@ module.exports = [ // middleware order name: 'swaggerUI', factory: require('./swaggerUI') }, - { - name: 'formParser', - factory: require('./formParser') - }, - { - name: 'bodyParser', - factory: require('./bodyParser') - }, - { - name: 'logger', - factory: require('./logger') - }, { name: 'cors', factory: require('./cors') @@ -39,6 +27,18 @@ module.exports = [ // middleware order name: 'etag', factory: require('./etag') }, + { + name: 'formParser', + factory: require('./formParser') + }, + { + name: 'bodyParser', + factory: require('./bodyParser') + }, + { + name: 'logger', + factory: require('./logger') + }, { name: 'router', factory: require('./router') @@ -67,4 +67,4 @@ module.exports = [ // middleware order name: 'requestHandler', factory: require('./requestHandler') } -]; +]; \ No newline at end of file