From a2b01163edcc4de167f8e8ad884652f43cf5ad8d Mon Sep 17 00:00:00 2001 From: Sergey Peshkov Date: Sat, 13 Jun 2020 18:45:40 +0300 Subject: [PATCH] fix(logging): write only specific fields for user logging (#81) --- lib/morgan.js | 13 +++++++++++-- package.json | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/morgan.js b/lib/morgan.js index 5beb9069..3904938f 100644 --- a/lib/morgan.js +++ b/lib/morgan.js @@ -1,15 +1,24 @@ const morgan = require('morgan'); +const _ = require('lodash'); const log = require('./logger'); module.exports = morgan((tokens, req, res) => { + const user = req.user + ? _.pick(req.user, ['id', 'username', 'first_name', 'last_name', 'email']) + : undefined; + + const body = _.isEmpty(req.body) + ? undefined + : req.body; + log.info({ method: tokens.method(req, res), url: tokens.url(req, res), status: tokens.status(req, res), length: tokens.res(req, res, 'content-length'), 'response-time': tokens['response-time'](req, res), - user: req.user, - body: req.body + user, + body }, 'Request processed'); }); diff --git a/package.json b/package.json index 76754540..c9903ad1 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "express": "^4.17.1", "express-promise-router": "^3.0.3", "joi": "^14.3.1", + "lodash": "^4.17.15", "moment": "^2.24.0", "morgan": "^1.10.0", "on-finished": "^2.3.0",