bithavoc / express-winston

express.js middleware for winstonjs
https://www.npmjs.com/package/express-winston
MIT License
796 stars 187 forks source link

res.statusCode returns unexpected string #204

Closed Enado95 closed 5 years ago

Enado95 commented 5 years ago

When I use the res.statusCode in the msg property, it returns a not so favorable response

code: msg: 'Statuscode {{res.statusCode}}'

result of res.statusCode:

"message": "\u001b[33m401\u001b[39m"

Is there anyway to go around this?

Enado95 commented 5 years ago

I'm trying to use it like so as well: 'Test' + ('{{res.statusCode}}'== '401' ? ' ' : ' Username: {{req.user.preferred_username}}' )

rosston commented 5 years ago

Are you talking about the \u001b[33m style strings? Those are color codes for the terminal. Do you have colorize: true set in your logger options? The color codes should go away if the colorize option is falsy.

Enado95 commented 5 years ago

I do have it set to true. Will try again later on and advise of results.

Enado95 commented 5 years ago

Okay so setting the colorize to false gives me back the statusCode but my conditional statement wont work: msg: 'Test' + ('{{res.statusCode}}'== '401' ? ' ' : ' Username: {{req.user.preferred_username}}' )

So i want to get an empty string if the statusCode is 401 otherwise I'll print the username from the user object that I've populated. Can this be done ?

rosston commented 5 years ago

There's probably a way to make what you want work with a mustache template string, but it might get pretty difficult to understand since I think you'd need your ternary to be inside the {{}}. You might want to switch to using a function for msg instead (supported as of v2.6.0). Something like this would probably do what you want?

msg: (req, res) => 'Test' + (res.statusCode == 401 ? ' ' : ` Username: ${req.user.perferred_username}`)
Enado95 commented 5 years ago

Thanks a million! It works just as expected.