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.

Colorize doesn't work on transports.Console

See original GitHub issue

I created a shared logger successfully, but it prints all white, even though I’ve set colorize to true. And as a test, My Label successfully prints out in red.

// Logging with Winston
const { red } = require('colors')
const os = require('os')
const fs = require('fs')
const path = require('path')
const config = require('../data/config/config')
const winston = require('winston')
const { createLogger, format, transports } = winston
const { combine, timestamp, label, printf } = format
const tsFormat = () => (new Date()).toLocaleTimeString()
const myFormat = printf(info => {
  return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`
})

const logDir = path.resolve(os.homedir(), '.curator-server')

if (!fs.existsSync(logDir)) {
  fs.mkdirSync(logDir)
}

const logger = createLogger({
  format: combine(
    label({ label: red('My Label') }),
    timestamp(),
    myFormat
  ),
  transports: [
    new transports.Console({ level: config.logLevel, humanReadableUnhandledException: true, colorize: true }),
    new transports.File({ filename: path.resolve(logDir, 'info.log'), level: 'info' }),
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error' })
  ]
})

module.exports = logger

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
mempfcommented, Oct 16, 2017

Right, so I’ve looked more into this. You can’t “format.colorize()” after you use “myFormat” since the modifications colorize does would not be seen in the final message.

You would need to add colorize and myFormat to the format specifiers of each transport instead of for the whole logger.

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

const logger = createLogger({
  format: combine(
    label({ label: red('My Label') }),
    timestamp()
  ),
  transports: [
    new transports.Console({
      format: combine(
        colorize(),
        myFormat
      ),
      level: 'silly' }),
    new transports.File({ format: myFormat, filename: path.resolve(logDir, 'info.log'), level: 'info' }),
    new transports.File({ format: myFormat, filename: path.resolve(logDir, 'error.log'), level: 'error' })
  ]
})
16reactions
mempfcommented, Oct 15, 2017

Try moving the colorize to a format property inside your Console constructor: new transports.Console({ level: config.logLevel, format: colorize() })

Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston 3.0 colorize whole output on console - Stack Overflow
I have already tried different NPM Packages like winston color and winston-console-formatter, but they don't seem to work.
Read more >
How to use the winston.format.colorize function in winston - Snyk
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... function createConsoleTransport (args, logLvl) {...
Read more >
A Complete Guide to Winston Logging in Node.js - Better Stack
The following transport options are available in Winston by default: Console: output logs to the Node.js console. File: store log messages to one...
Read more >
logform.colorize JavaScript and Node.js code examples
createLogger({ level: appConfig.logs.level || 'silly', format: combine(colorize(), timestamp(), myFormat), transports: [ new transports.Console({ colorize: ...
Read more >
winston - npm
Note that the default logger doesn't have any transports by default. ... to be written in winston; Working with multiple Loggers in winston....
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