morgan icon indicating copy to clipboard operation
morgan copied to clipboard

:status color when not dev too.

Open ktjd123 opened this issue 6 years ago • 5 comments

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.

ktjd123 avatar Feb 12 '19 03:02 ktjd123

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 avatar Apr 11 '19 01:04 dotconnor

@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.

Boboss74 avatar Apr 25 '19 10:04 Boboss74

Any reason not to make this colouring default for status token?

vasanthv avatar Oct 03 '19 05:10 vasanthv

@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

t-fritsch avatar Oct 25 '19 12:10 t-fritsch

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.

ryhinchey avatar Feb 28 '20 15:02 ryhinchey