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

I can not colorize only the console logger #1384

Closed
1 of 2 tasks
sandrocsimas opened this issue Jun 28, 2018 · 3 comments
Closed
1 of 2 tasks

I can not colorize only the console logger #1384

sandrocsimas opened this issue Jun 28, 2018 · 3 comments

Comments

@sandrocsimas
Copy link

sandrocsimas commented Jun 28, 2018

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v8.10.0
  • Operating System? Linux
  • Language? ES6

What is the problem?

I can not colorize only the console logger.

const fileLogger = winston.createLogger({
  level,
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`),
  ),
  transports: [
    new winston.transports.Console({
      format: winston.format.colorize(),
    }),
    new winston.transports.File({
      filename: path.resolve('project.log')
    }),
  ]
});

Console output:

2018-06-28T16:50:50.414Z - info: Using development environment settings
2018-06-28T16:50:50.417Z - info: Debug mode is ON
2018-06-28T16:50:50.417Z - info: Configuring routes
2018-06-28T16:50:50.423Z - info: Intercambio Backend server is listening on port 3000

The console output above does not colorize the message.

What do you expect to happen instead?

I want the colorized message only in the console logger. The file should keep printing without color.

@sandrocsimas sandrocsimas changed the title Cannot colorize only console logger Can not colorize only console logger Jun 28, 2018
@sandrocsimas sandrocsimas changed the title Can not colorize only console logger I can not colorize only the console logger Jun 28, 2018
@ChrisAlderson
Copy link
Member

The formats are applied in order meaning the logger formats are applied first, then the transport formats are applied. The following example would get the wanted result:

const myFormat = winston.format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`);

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.timestamp(),
  transports: [
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize(),
        myFormat,
      )
    }),
    new winston.transports.File({
      filename: path.resolve('project.log'),
      format: myFormat,
    }),
  ]
});

@dimadk24
Copy link

To get out of the box easy colorizing in dev console with less code check out related a comment in a similar issue: #1135 (comment)

@greatertomi
Copy link

greatertomi commented Aug 4, 2023

This works for me.

const colorsConfig = {
  error: 'red',
  warn: 'yellow',
  info: 'green',
  debug: 'cyan'
};

const colorizer = winston.format.colorize({ all: true, colors: colorsConfig });

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.printf(({ level, message, timestamp, ...args }) => {
      const coloredLevel = colorizer.colorize(level, `[${level.toUpperCase()}]`);
      return `${timestamp} ${coloredLevel}: ${message} ${
        Object.keys(args).length > 0 ? JSON.stringify(args, null, 2) : ''
      }`;
    })
  ),
 ...
});

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

No branches or pull requests

4 participants