Skip to content

Instantly share code, notes, and snippets.

@jdmar3
Forked from cgmartin/logging-middleware.js
Last active March 8, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdmar3/4c2df3ffc8966c0ed666f5fa0b7fdefa to your computer and use it in GitHub Desktop.
Save jdmar3/4c2df3ffc8966c0ed666f5fa0b7fdefa to your computer and use it in GitHub Desktop.
Morgan JSON log format example
'use strict';
const morgan = require('morgan');
module.exports = function jsonLog() {
return morgan(jsonFormat);
};
function jsonFormat(tokens, req, res) {
return JSON.stringify({
'remote-address': tokens['remote-addr'](req, res),
// 'remote-user': tokens['remote-user'](req, res),
'time': tokens['date'](req, res, 'iso'),
'method': tokens['method'](req, res),
'url': tokens['url'](req, res),
'http-version': tokens['http-version'](req, res),
'status-code': tokens['status'](req, res),
'content-length': tokens['res'](req, res, 'content-length'),
'referrer': tokens['referrer'](req, res),
'user-agent': tokens['user-agent'](req, res),
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment