Last active
December 23, 2015 00:24
-
-
Save jeonghwan-kim/065dabf47987e729735a to your computer and use it in GitHub Desktop.
This file contains 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
var morgan = require('morgan'); | |
/** | |
* morgan wrapper | |
* @returns {morgan} | |
*/ | |
module.exports = function setLogger() { | |
// http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html | |
var red = '\x1B[31m', | |
green = '\x1B[32m', | |
yellow = '\x1B[33m', | |
cyan = '\x1B[36m', | |
white = '\x1B[37m', | |
endColor = '\033[0m'; | |
// Redefind method token | |
morgan.token('method', function (req, res) { | |
var color; | |
if (req.method === 'GET') color = green; | |
else if (req.method === 'POST') color = cyan; | |
else if (req.method === 'PUT') color = yellow; | |
else if (req.method === 'DELETE') color = red; | |
else color = white; | |
return color + req.method + endColor; | |
}); | |
// Redefine status token | |
morgan.token('status', function (req, res) { | |
var color; | |
if (res.statusCode < 300) color = green; | |
else if (res.statusCode < 400) color = cyan; | |
else if (res.statusCode < 500) color = yellow; | |
else if (res.statusCode < 600) color = red; | |
else color = white; | |
return color + res.statusCode + endColor; | |
}); | |
// Create a token for request body | |
morgan.token('body', function (req, res) { | |
return white + 'body: ' + JSON.stringify(req.body) + endColor; | |
}); | |
return morgan(':method :url :status :response-time ms :body'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: