Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:status color when not dev too. #186

Open
ktjd123 opened this issue Feb 12, 2019 · 5 comments
Open

:status color when not dev too. #186

ktjd123 opened this issue Feb 12, 2019 · 5 comments

Comments

@ktjd123
Copy link

ktjd123 commented Feb 12, 2019

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.

@dotconnor
Copy link

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

morgan/index.js

Line 183 in 12a48c5

morgan.format('dev', function developmentFormatLine (tokens, req, res) {

@Boboss74
Copy link

Boboss74 commented Apr 25, 2019

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

@vasanthv
Copy link

vasanthv commented Oct 3, 2019

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

@t-fritsch
Copy link

@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

@ryhinchey
Copy link
Contributor

ryhinchey commented Feb 28, 2020

There's now a PR open for this #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants