trojek
trojek

Reputation: 3228

How to enable colors in winston loggin system?

What is the simplest way to enable default colors logs messages in winston?

For example, if I use winston as follows:

let winston = require('winston')

winston.info('Info message')
winston.error('Error message')

I expect that the colors of info and error message will be respectively yellow and red?

Upvotes: 0

Views: 107

Answers (3)

Oliver Wagner
Oliver Wagner

Reputation: 406

You can use the colorize function like so:

const { createLogger, format, transports } = require("winston");

const logger = createLogger({
    format: format.colorize({ all: true }),
    transports: [new transports.Console()],
})

This will add the standard winston colors for each of the log levels.

If you want to define your own colors, then its

const { createLogger, format, transports, addColors } = require("winston");

addColors({
  info: "green", // fontStyle color
  warn: "bold yellow",
  error: "bold red",
  debug: "bold red cyanBG”, //font style, font color, background color
});

const logger = createLogger({
    level: “debug”,
    format: format.colorize({all: true}),
    transports: [new transports.Console()]
})

And then you just export the module and use it everywhere

Upvotes: 0

trojek
trojek

Reputation: 3228

You can use:

let winston = require('winston')
winston.cli();

Upvotes: 1

lumio
lumio

Reputation: 7575

Even though info won't be yellow I would suggest you use winston-color:

const logger = require( 'winston-color' );
logger.info( 'Info message' ); // Will print info in green
logger.error( 'Error message' ); // Will print error in red

Upvotes: 1

Related Questions