Skip to content

Commit

Permalink
fix(logging): write only specific fields for user logging (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff authored Jun 13, 2020
1 parent e157128 commit a2b0116
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 @@ -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",
Expand Down

0 comments on commit a2b0116

Please sign in to comment.