winstonjs / winston

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

colorize (only with console transport) doest not work with custom format #1774

Open Minyar2004 opened 4 years ago

Minyar2004 commented 4 years ago

Please tell us about your environment:

What is the problem?

Problem with colorize() `

const winston = require('winston');

const customLogFormat = winston.format.printf(info => {
  return `${info.timestamp} [${info.module}] ${info.level}: ${info.message} `;
});

const logger = module.exports = winston.createLogger({
  level: 'silly',
  format: winston.format.combine(
    winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    winston.format.errors({ stack: true }),
    winston.format.splat(),
    customLogFormat
  ),
  defaultMeta: { module: 'nomModule1' },
  transports: [
    new winston.transports.File({
      filename: 'fileLog.log'
    }),
    new winston.transports.Console({
      format: winston.format.colorize()
    })
  ]
});

logger.debug({
  message: 'myMessage',
  a: 'a',
  b: 'b'
});

`

What do you expect to happen instead?

Expected behavior: levels should be colored in console transport, but with the code above colorize() does not work and level are not colored...

Other information

swapnil0545 commented 4 years ago

It does work. You are giving the format property on global level. Give it inside the transport property like shown here https://github.com/winstonjs/winston#common-transport-options