question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Console logging is not colorized.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

145reactions
Xeoncrosscommented, 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

130reactions
abrakadobrcommented, Nov 28, 2017

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()
          )
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Colorful Console Message | SamanthaMing.com
Put this in your browser console console.log('%cHello', 'color: green; background: yellow; ... The text before the directive will not be affected.
Read more >
google chrome - Colors in JavaScript console - Stack Overflow
Older versions of Chrome do not allow you to get console.log() s to show in a specific color programmatically, but calling console.error() will...
Read more >
How to Log to the Console in Color - Baeldung
In this article, we'll see how to add color to our logs for consoles such as the Visual Studio Code terminal, Linux, and...
Read more >
How to Add Colors in JavaScript Console Log Output?
We can add CSS styles to console log output with the %c tag. For instance, we can write: console.log('%c hello world ', 'background:...
Read more >
How to Enable Color in Spring Boot Console Log
On most systems color coding works automatically. However if you are using a Mac terminal, you will find that no color coding is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found