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

Console logging is not colorized. #1135

Closed
alvaro1728 opened this issue Nov 10, 2017 · 23 comments · Fixed by #1258
Closed

Console logging is not colorized. #1135

alvaro1728 opened this issue Nov 10, 2017 · 23 comments · Fixed by #1258

Comments

@alvaro1728
Copy link

alvaro1728 commented Nov 10, 2017

I'm trying to colorize the console output and it's not working. The output is all the same (white) color. I'm on 3.0.0-rc1.

const winston = require('winston');
const logLevels = {
  levels: {
    error: 0,
    warn: 1,
    info: 2,
    http: 3,
    sql: 4,
    debug: 5
  },
  colors: {
    error: "red",
    warn: "darkred",
    info: "black",
    http: "green",
    sql: "blue",
    debug: "gray"
  }
};
winston.addColors(logLevels);
const logger = winston.createLogger({...});
logger.add(new winston.transports.Console({colorize: true}));

I also tried the suggestion here but that's not working either:

logger.add(new winston.transports.Console({format: winston.format.combine(formatter, winston.format.colorize())}));

Thanks,
Alvaro

@xeoncross
Copy link

xeoncross commented Nov 13, 2017

I was able to get it partially working using the documentation for logform.

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf(info => `${info.timestamp} [${info.level}]: ${info.message}`),
  );

Not sure how to handle dumping the JSON arguments.

Update here is what I worked out:

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf((info) => {
      const {
        timestamp, level, message, ...args
      } = info;

      const ts = timestamp.slice(0, 19).replace('T', ' ');
      return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
    }),
  );

screen shot 2017-11-13 at 10 55 05 am

@microcipcip
Copy link

After two hours I managed to get the colors working, thanks @xeoncross !

@abrakadobr
Copy link

started winston and also got console without colors...
i was using next transport defining code:

new winston.transports.Console({
  format: winston.format.simple()
})

after reading this thread i did like this:

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.simple(),
            winston.format.colorize()
          )
})

and got no difference.
so i changed position of colorize format to 1st and got colors working

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.colorize(),
            winston.format.simple()
          )
})

@Fire-Brand
Copy link

Thanks @abrakadobr ! that did the trick.

@goozo
Copy link

goozo commented Dec 15, 2017

nothing here worked for me, still no colors, and shifting colorize to the top caused the info field to log encoded strings

@xeoncross
Copy link

I created a gist that shows how to use winston & morgan logging in a simple expressjs app.

@spconway
Copy link

Thanks @xeoncross! Adding winston.format.colorize() to my format worked. Interesting that this isn't in the docs. Or at least I couldn't find it in the docs.

@natesilva
Copy link

I love the new flexibility, but I hope it gets better documentation. I just spent too long getting this to work. A plug-n-play example for those of us who were using winston.cli() would go a long way, something similar to @xeoncross’s example.

morenoh149 added a commit to morenoh149/winston that referenced this issue Apr 4, 2018
indexzero pushed a commit that referenced this issue Apr 6, 2018
@jtiscione
Copy link

Anyone know how to do it with the current release version? A release candidate version is not an option.

@markhealey
Copy link

In reading the code you can also send all to the colorize as an option to colorize the entire log.

colorize({ all: true })

@artemcarabash
Copy link

@markhealey I already removed winston dependency from my package.json and then I read your message. Thank you 👍

@matiangul
Copy link

Thx @xeoncross. Take a look at: https://github.com/winstonjs/logform/blob/master/timestamp.js. You can format timestamp with format opt param. But be careful with timezone

format.combine(
    format.colorize(),
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    format.align(),
    format.printf(info => {
        const { timestamp, level, message, ...extra } = info;

        return `${timestamp} [${level}]: ${message} ${
            Object.keys(extra).length ? JSON.stringify(extra, null, 2) : ''
        }`;
    }),
)

@tommuhm
Copy link

tommuhm commented Oct 25, 2018

format.colorize({all:true} does not really work if timestamp is included, here is a working example to colorize the whole line:

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
  level: 'debug',
  format: combine(
    winston.format.timestamp(),
    winston.format.simple(),
    winston.format.printf(msg => 
      colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
    )
  ),
  transports: [
    new transports.Console(),
  ]

});

@dengue8830
Copy link

dengue8830 commented Nov 14, 2018

check my full featured logger

@Gilbert1391
Copy link

Thanks @abrakadobr, your solution worked for me.

@indexzero
Copy link
Member

@tommuhm I see the defect now. There isn't a built-in format option for colorize if one does indeed want to colorize the entire printf. Does this make sense to you?

const { createLogger, format, transports } = require('../');

const logger = createLogger({
  level: 'debug',
  format: format.combine(
    format.timestamp(),
    format.simple(),
    format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`),
    format.colorize({ all: true })
  ),
  transports: [
    new transports.Console(),
  ]
});

logger.error('wowza');

The tricky bit is having the colorizer known when an info has been "serialized" (i.e. when info[MESSAGE] has been set to a string). Follow-up PR to logform shortly – thanks for digging through it!

@SravanTurbo
Copy link

@xeoncross we can also achieve the same in a much simpler way

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
	return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
	format: combine(colorize(), timestamp(), myFormat),
	transports: [new transports.Console()]
});

reference: https://www.npmjs.com/package/winston#using-custom-logging-levels

@rafiaCs
Copy link

rafiaCs commented Aug 22, 2019

i am using winston for logging in cloudwatch and trying to add color but instead of color symbols appear.. I try all solutions mention above. below is my code

const winston = require('winston');
const {transports, format, createLogger  } = winston;
const { combine,errors,timestamp} = format;

const colorizer = winston.format.colorize();


const logger = createLogger({
    level:  process.env.LOG_LEVEL,
    prettyPrint : true,
    format: combine(
        winston.format.timestamp(),
        winston.format.simple(),
        winston.format.printf(msg =>
            colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
        )
    ),
    transports: [
        new transports.Console()
    ]
});

module.exports = {logger};

and in cloudwatch console output is

�[34m2019-08-22T13:00:03.325Z - debug: res by id: [
{
    "isActive": true,
    "value": "fNUMheWiwXayKsaYCbUeA7Gg7BtEPIUbakB56XH1",
    "id": "oehlwqlcve",
    "userId": "6b347b41-ddef-4842-83a0-1cd5ca358482"
}
]�[39m

this [39m apear instead of color. Any suggestion?

@JackyGohSG
Copy link

-22T13:00:03.325Z - debug: res by id: [

I'm having the same issue as well. Did you manage to fix it?

Mizumaki pushed a commit to Mizumaki/winston that referenced this issue Jun 11, 2020
@mchimczak
Copy link

-22T13:00:03.325Z - debug: res by id: [

I'm having the same issue as well. Did you manage to fix it?

Any updates on that? Did you solve it somehow?

@r3sdev
Copy link

r3sdev commented Jan 30, 2021

@xeoncross we can also achieve the same in a much simpler way

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
	return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
	format: combine(colorize(), timestamp(), myFormat),
	transports: [new transports.Console()]
});

reference: https://www.npmjs.com/package/winston#using-custom-logging-levels

That did the trick for me in iTerm on MacOS

@bharukarupesh11
Copy link

@greatertomi
Copy link

greatertomi commented Aug 4, 2023

Reading this in 2023?

The solution here didn't work for me. This is how I could get it to work.

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

Successfully merging a pull request may close this issue.