Skip to content

Commit

Permalink
Add coloring option for status token
Browse files Browse the repository at this point in the history
Coloring status makes sense for whatever format user wants, not just the internal `dev` one. So using `:status[color]` gives that ability.

As for collaterally removed memoization for `dev` format, well, wouln't it be better to memoize all formats that go through `compile`, not just the `dev` one? If that's the case, I'll do that separately.

resolves expressjs#171
  • Loading branch information
st-sloth committed Jul 14, 2018
1 parent 4dd1180 commit bb64cb0
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ var CLF_MONTH = [

var DEFAULT_BUFFER_DURATION = 1000

/**
* Placeholder that replaces empty values in resulting log line
* @private
*/

var EMPTY_VALUE_PLACEHOLDER = '-'

/**
* Create a logger middleware.
*
Expand Down Expand Up @@ -180,30 +187,7 @@ morgan.format('tiny', ':method :url :status :res[content-length] - :response-tim
* dev (colored)
*/

morgan.format('dev', function developmentFormatLine (tokens, req, res) {
// get the status code if response written
var status = headersSent(res)
? res.statusCode
: undefined

// get status color
var color = status >= 500 ? 31 // red
: status >= 400 ? 33 // yellow
: status >= 300 ? 36 // cyan
: status >= 200 ? 32 // green
: 0 // no color

// get colored function
var fn = developmentFormatLine[color]

if (!fn) {
// compile
fn = developmentFormatLine[color] = compile('\x1b[0m:method :url \x1b[' +
color + 'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m')
}

return fn(tokens, req, res)
})
morgan.format('dev', '\x1b[0m:method :url :status[color] \x1b[0m:response-time ms - :res[content-length]\x1b[0m')

/**
* request url
Expand Down Expand Up @@ -260,10 +244,25 @@ morgan.token('date', function getDateToken (req, res, format) {
* response status code
*/

morgan.token('status', function getStatusToken (req, res) {
return headersSent(res)
morgan.token('status', function getStatusToken (req, res, format) {
var status = headersSent(res)
? String(res.statusCode)
: undefined

if (format === 'color') {
// get status color
var color = status >= 500 ? 31 // red
: status >= 400 ? 33 // yellow
: status >= 300 ? 36 // cyan
: status >= 200 ? 32 // green
: 0 // no color

// using placeholder here since `compile` adds it if token function returns falsy,
// but this coloring branch returns color data anyway and value won't be eligible for substitute
return '\x1b[' + color + 'm' + (status || EMPTY_VALUE_PLACEHOLDER)
}

return status
})

/**
Expand Down Expand Up @@ -384,7 +383,9 @@ function compile (format) {
tokenArguments += ', ' + String(JSON.stringify(arg))
}

return '" +\n (' + tokenFunction + '(' + tokenArguments + ') || "-") + "'
return '" +\n (' +
tokenFunction + '(' + tokenArguments + ') || "' + EMPTY_VALUE_PLACEHOLDER +
'") + "'
}) + '"'

// eslint-disable-next-line no-new-func
Expand Down

0 comments on commit bb64cb0

Please sign in to comment.