Link Search Menu Expand Document

Custom Format Function

morgan(function (tokens, req, res) {
  return [
    tokens.method(req, res),
    tokens.url(req, res),
    tokens.status(req, res),
    tokens.res(req, res, 'content-length'), '-',
    tokens['response-time'](req, res), 'ms'
  ].join(' ')
})

Log request body

morgan.token('body', function (req, res) { return JSON.stringify(req.body) });

app.use(morgan(':method :url :status :response-time ms - :res[content-length] :body - :req[content-length]', { 
  stream: logger.successLogStream,
  skip: function (req, res) { return res.statusCode >= 400 }
 }));

 app.use(morgan(':method :url :status :response-time ms - :res[content-length] :body - :req[content-length]', { 
  stream: logger.errorLogStream,
  skip: function (req, res) { return res.statusCode < 400 }
 }));

Copyright © 2020 Thence LLC