Skip to content

Commit

Permalink
fix(logging): write only specific fields for user logging (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff authored Jun 13, 2020
1 parent 53eb820 commit 7c024cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/morgan.js
Original file line number Diff line number Diff line change
@@ -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');
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"express-query-boolean": "^2.0.0",
"faker": "^4.1.0",
"file-type": "^14.5.0",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"morgan": "^1.9.1",
"multer": "^1.4.2",
Expand Down

0 comments on commit 7c024cd

Please sign in to comment.