winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.64k stars 1.8k forks source link

Regex colors code appears with error message \u001b[31merror\u001b[39m #1941

Open Dipikesh opened 3 years ago

Dipikesh commented 3 years ago

Please tell us about your environment:

What is the problem?

Error response of code is including regex color code like this. "message": "\u001b[31merror\u001b[39m"

Here is my code.

const winston = require('winston')

const levels = {
  error: 0,
  warn: 1,
  info: 2,
  http: 3,
  debug: 4,
}
const level = () => {
  const env = process.env.NODE_ENV || 'development'
  const isDevelopment = env === 'development'
  return isDevelopment ? 'debug' : 'warn'
}

const colors = {
  error: 'red',
  warn: 'yellow',
  info: 'blue',
  http: 'magenta',
  debug: 'green',
}

winston.addColors(colors)

// Chose the aspect of your log customizing the log format.
const format = winston.format.combine(
  // Add the message timestamp with the preferred format
  winston.format.timestamp({ format: 'DD/MM/YYYY HH:mm:ss' }),
  // Tell Winston that the logs must be colored
  winston.format.colorize({ all: true }),
  // Define the format of the message showing the timestamp, the level and the message
  winston.format.printf(
    (info) => `${info.timestamp} ${info.level}: ${info.message}`,
  ),
)
const transports = [
  // Allow the use the console to print the messages
  new winston.transports.Console(),
  // Allow to print all the error level messages inside the error.log file
  new winston.transports.File({
    filename: 'logs/error.log',
    level: 'error',
  }),
  // (also the error log that are also printed inside the error.log(
  new winston.transports.File({ filename: 'logs/all.log' }),
]

const Logger = winston.createLogger({
  level: level(),
  levels,
  format,
  transports,
})

module.exports = Logger

If i comment out winston.format.colorize({all:true}), the error response has no regex color code with it , it works fine but the color of logger is disappeared.

What do you expect to happen instead?

Error response should not have any color code with it and i can colorize my console log with it like this, "message":"Error"

Other information

dohard-ma commented 2 years ago

This problem still exists in version 3.8.1

mrobst commented 1 year ago

This issue also causes incorrect output in https://github.com/willmorgan/winston-azure-application-insights - the AI module works but has a lookup to map winston log levels to AI log levels. If the format.colorize() option is set then the log level is not found correctly and always defaults to 1 (info). This in turn prevents exceptions being logged to AI (only trace messages with severity = information).

SergioSuarezDev commented 9 months ago

same in latest version 3.11.0

deathcrafter commented 3 weeks ago

Apparently, you do it like this:

new winston.transports.Console({
  level: "debug",
  format: combine(timestamp(), prettyPrint({ depth: 4, colorize: true })),
}),

Notice the prettyPrint({ depth: 4, colorize: true }) part.

Now this doesn't do what colorize does though. It just colors all values green.