Created
May 24, 2015 01:43
-
-
Save cgmartin/913bb12097ff07132597 to your computer and use it in GitHub Desktop.
Morgan JSON log format example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var morgan = require('morgan'); | |
var os = require('os'); | |
morgan.token('conversation-id', function getConversationId(req) { | |
return req.conversationId; | |
}); | |
morgan.token('session-id', function getSessionId(req) { | |
return req.sessionId; | |
}); | |
morgan.token('instance-id', function getInstanceId(req) { | |
return req.instanceId; | |
}); | |
morgan.token('hostname', function getHostname() { | |
return os.hostname(); | |
}); | |
morgan.token('pid', function getPid() { | |
return process.pid; | |
}); | |
module.exports = function loggingMiddleware() { | |
return morgan(jsonFormat); | |
}; | |
function jsonFormat(tokens, req, res) { | |
return JSON.stringify({ | |
'remote-address': tokens['remote-addr'](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), | |
'conversation-id': tokens['conversation-id'](req, res), | |
'session-id': tokens['session-id'](req, res), | |
'hostname': tokens['hostname'](req, res), | |
'instance': tokens['instance-id'](req, res), | |
'pid': tokens['pid'](req, res) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment