morgan
morgan copied to clipboard
:status color when not dev too.
I really like the colored status but when I use not dev but custom string, :status is not colored. hope this is colored too because it's cool.
Always appreciate your works.
You can reimplement that status
token yourself:
morgan.token(`status`, (req, res) => {
const status = (typeof res.headersSent !== `boolean`
? Boolean(res._header)
: res.headersSent)
? res.statusCode
: undefined
// get status color
const color =
status >= 500
? 31 // red
: status >= 400
? 33 // yellow
: status >= 300
? 36 // cyan
: status >= 200
? 32 // green
: 0 // no color
return `\x1b[${color}m${status}\x1b[0m`
})
Copied from https://github.com/expressjs/morgan/blob/12a48c5598d67f67bc09ceac393176200bf65865/index.js#L183
@dotconnor thanks
I replaced the undefined
by '-'
and it's working as expected.
I preferred to use an other name for the token instead of using an existing one. I think it's safer.
Any reason not to make this colouring default for status
token?
@vasanthv I'm quite new to morgan, so maybe I'm not right but I guess this is because :status
is commonly used when writing to files. Colors are only intended to be used when outputting to terminal.
Trying to use 'dev'
when writing to file results in something like this :
[0mGET /urldoesntexist [33m404 [0m55.714 ms - -[0m
There's now a PR open for this https://github.com/expressjs/morgan/pull/227
but I guess this is because
:status
is commonly used when writing to files. Colors are only intended to be used when outputting to terminal.
This is exactly right. The colors add a lot of extra text around the actual status code which would make it very difficult to parse in a file.