Winston won't log in color

889 Views Asked by At

This doesn't print to the console in color. Everything is white. I've been Googling for hours. I'm running Ubuntu 17.

// Logging with Winston
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 tsFormat = () => (new Date()).toLocaleTimeString()

const logDir = path.resolve(os.homedir(), '.test-logs')

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

const logger = createLogger({
  format: format.combine(
    format.splat(),
    format.simple()
  ),
  transports: [
    new transports.Console({ timestamp: tsFormat, level: config.logLevel, 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

I don't get any error messages or anything. It's just white.

1

There are 1 best solutions below

0
On BEST ANSWER

Edit: After answering my own question, I was told through a conversation on their github page that you can actually pass formats to the transports themselves.

const logger = createLogger({
  transports: [
    new transports.Console({ level: config.logLevel, format: customFormat, colorize: true, handleExceptions: true, humanReadableUnhandledException: true }),
    new transports.File({ filename: path.resolve(logDir, 'server.log'), level: 'silly' }),
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error', handleExceptions: true })
  ]
})

Original Answer

You have to add colorize to the format when instantiating the logger. There was no example of this in their documentation. Their reference to a repo called logForm is done exactly as I just did. It should be a hyperlink instead. I might do a PR. The repo is here

I had to go to their examples directory to find this example.

// Logging with Winston
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, colorize } = format
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: 'Curator Server' }),
    colorize(), // Here is where the color happens
    timestamp(),
    myFormat
  ),
  transports: [
    new transports.Console({ level: config.logLevel, colorize: true, handleExceptions: true, humanReadableUnhandledException: true }),
    new transports.File({ filename: path.resolve(logDir, 'info.log'), level: 'info' }),
    new transports.File({ filename: path.resolve(logDir, 'error.log'), level: 'error', handleExceptions: true })
  ]
})

module.exports = logger