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

Colourized timestamp #112

Open
amitav13 opened this issue Sep 22, 2020 · 2 comments
Open

Colourized timestamp #112

amitav13 opened this issue Sep 22, 2020 · 2 comments

Comments

@amitav13
Copy link

amitav13 commented Sep 22, 2020

I'm currently using a format like so:

const formatter = require("winston").format;
new winston.transports.Console({
          format: formatter.combine(
            formatter.timestamp(),
            formatter.colorize(),
            formatter.align(),
            formatter.printf(
              (info) => `[${info.timestamp}] ${info.level}: ${info.message}`
            )
          ),
        })

I wanted to set a colour to my timestamp, I don't see a way of doing that at this point. Adding this to the colors object would be ideal. Something like:

formatter.colorize({
              colors: {
                timestamp: "red",
              },
            })

I have a small recommendation for the default color for timestamp too: #666666, in case anyone's listening :)

@AurangAsif
Copy link

This is already possible and I am using it in development.

You need to install chalk.

Then you can do:

const chalk = require('chalk');

const { combine, timestamp, colorize, align, printf } = winston.format;

new winston.transports.Console({
  format: combine(
    timestamp(),
    colorize(),
    align(),
    printf(
      ({ timestamp, level, message }) => chalk`[{keyword('lightblue') ${timestamp}}] ${level}: ${message}`
    )
  )
});

See chalk's documentation on tagged template literals https://github.com/chalk/chalk#tagged-template-literal

In case you're curious, this is what i'm using currently:

if (process.env.NODE_ENV !== 'production') {
  logger.add(new transports.Console({
    format: combine(
      format.splat(),
      format.colorize(),
      format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      format.ms(),
      printf(({ timestamp, level, message, ms }) => {
        return chalk`[{keyword('lightblue') ${timestamp}}] [${level}]: ${message} {keyword('magenta') ${ms}}`
      })
    )
  }));
}

@jagcruz
Copy link

jagcruz commented Sep 5, 2021

This is already possible with the winston-timestamp-colorize module, as the name says it allows you to colorize the timestamp. Example:

const winston = require("winston");
const timestampColorize = require("winston-timestamp-colorize");

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.splat(),
    winston.format.align(),
    winston.format.timestamp(),
    winston.format.colorize(),
    timestampColorize({ color: "red" }),
    winston.format.printf(
      info => `${info.timestamp} [${info.level}]:${info.message}`
    )
  ),
  level: "debug",
  transports: [new winston.transports.Console({})]
});

logger.debug("Hello world!");

Available colors are:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray
  • grey

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

3 participants