bithavoc / express-winston

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

Colorize option generates escaped color code #151

Closed chenfanggm closed 7 years ago

chenfanggm commented 7 years ago

Problem: option 'Colorize': true, generate color code instead of changing colors of the text.

Reason: _.template() will escape text

Resolution: Unknown yet

rosston commented 7 years ago

@chenfanggm Can you provide more of the logger/error logger config you're using? I just tested the following and can confirm that colors showed for me in the console:

expressWinston.logger({
  colorize: true,
  expressFormat: true,
  transports: [new winston.transports.Console({})]
})
chenfanggm commented 7 years ago

@rosston Thank you for coming back to me. Here's more details:

Config:

app.use(expressWinston.logger({
  winstonInstance: winston.instance,
  msg: '[{{req.requestStartTime}}] HTTP {{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms',
  level: __DEV__ ? 'debug' : 'info',
  meta: true,
  colorize: true,
  dynamicMeta: winston.dynamicMeta
}))

Winston config:

const logger = new (winston.Logger)({
  level: config.log.level,
  transports: [
    new (winston.transports.Console)({
      json: true,
      colorize: true
    })
  ]
})

Results: (look at the status code 200, it's around of escaped color codes)

"message": "[1497390792226] HTTP GET /api/v1/health \u001b[32m200\u001b[39m 2ms"
rosston commented 7 years ago

Hm, I think this just comes down to trying to output colors to JSON, where they'll always get escaped when output to console. I think the only possible resolution for this is to not use colorize: true (at the express-winston level) and JSON-outputting transports together.

I don't think there's any change to be made in the express-winston codebase. But I'll probably switch the top-level colorize: true in the readme over to false so that the example isn't broken by default.

chenfanggm commented 7 years ago

Thank you for your feedback. Appreciate! I'll close the issue now.