Winston doesn't pretty-print to console

I figured out the answer (the documentation is incorrect). If you use the constructor, and manually add transports, you can set options, both for winston, and for individual transports. Certain options need to be added to winston directly, while others need to be added to the transport.

E.g.:

var winston = require('winston');
var logger = new (winston.Logger)({
  levels: {
    trace: 0,
    input: 1,
    verbose: 2,
    prompt: 3,
    debug: 4,
    info: 5,
    data: 6,
    help: 7,
    warn: 8,
    error: 9
  },
  colors: {
    trace: 'magenta',
    input: 'grey',
    verbose: 'cyan',
    prompt: 'grey',
    debug: 'blue',
    info: 'green',
    data: 'grey',
    help: 'cyan',
    warn: 'yellow',
    error: 'red'
  }
});

logger.add(winston.transports.Console, {
  level: 'trace',
  prettyPrint: true,
  colorize: true,
  silent: false,
  timestamp: false
});

logger.add(winston.transports.File, {
  prettyPrint: false,
  level: 'info',
  silent: false,
  colorize: true,
  timestamp: true,
  filename: './nKindler.log',
  maxsize: 40000,
  maxFiles: 10,
  json: false
});

If you're using [email protected] then the accepted answer won't work. Try the following:

const winston = require("winston");
let date = new Date().toISOString();
const logFormat = winston.format.printf(function(info) {
  return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n`;
});
const logger = winston.createLogger({
  transports: [
    new winston.transports.Console({
      level: level,
      format: winston.format.combine(winston.format.colorize(), logFormat)
    })
  ]
});

The logs will have the following format:

It's colored BTW

2018-03-01T19:49:54.042Z-info: "----- Customer Details ------"

2018-03-01T19:49:54.042Z-info: [
    {
        "A": 1,
        "B": 2
    }
]

Here's a solution for Winston v3+

  let winstonFormat = winston.format.json();
  if (NODE_ENV == "development") {
    winstonFormat = winston.format.combine(winston.format.json(), winston.format.prettyPrint());
  }

  const log = winston.createLogger({
    level: "info",
    format: winstonFormat,
    defaultMeta: {app: "myapp"},
    transports: [
      new winston.transports.File({filename: "/dev/stderr", level: "warn"}),
      new winston.transports.File({filename: "/dev/stdout"}),
    ],
  });