Created
February 16, 2017 20:41
-
-
Save Arlen22/0d90389a9f593413aeca04af37f71d72 to your computer and use it in GitHub Desktop.
Morgan loggers
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
morgan.format('mydev', function developmentFormatLine(tokens, req, res) { | |
// get the status code if response written | |
var status = res._header | |
? res.statusCode | |
: 0 | |
var path = req.path.split('/'); | |
path = path[1] === 'col'; | |
// get status color | |
var color = status >= 500 ? 31 // red | |
: status >= 400 ? 33 // yellow | |
: status >= 300 ? 36 // cyan | |
: status >= 200 ? 32 // green | |
: 0 // no color | |
path = path ? 1 : 0; | |
var colorkey = [path, color].join('-'); | |
// get colored function | |
var fn = developmentFormatLine[colorkey] | |
if (!fn) { | |
var colorcode = '\x1b[' + color + 'm'; | |
var colorzero = '\x1b[0m'; | |
if(path) colorcode = colorzero = '\x1b[32m'; | |
// compile | |
fn = developmentFormatLine[colorkey] = morgan.compile( | |
colorzero + '[:date[clf]]:method ' + colorcode + ':status ' + colorzero + ' :padurl :response-time ms - :res[content-length]\x1b[0m' | |
) | |
} | |
return fn(tokens, req, res) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment