emilioriosvz
8/3/2016 - 11:25 AM

Wasted quite a bit of time today trying to get flatiron winston configured with custom levels and colors, so here's a working sample.

Wasted quite a bit of time today trying to get flatiron winston configured with custom levels and colors, so here's a working sample.

var winston = require('winston')
require('winston-mongodb')

// prepare some custom log levels
var customLevels = {
  levels: {
    debug: 0,
    info: 1,
    warning: 2,
    error: 3
  },
  colors: {
    debug: 'cyan',
    info: 'green',
    warning: 'yellow',
    error: 'red'
  }
}

// create the logger
var logger = module.exports = new (winston.Logger)({
  level: 'debug',
  levels: customLevels.levels,
  handleExceptions: true,
  //colors: customLevels.colors,
  transports: [
    // setup console logging
    new (winston.transports.Console)({
      level: 'debug',
      levels: customLevels.levels,
      handleExceptions: true,
      colorize: true
    }),
    // setup logging to mongodb
    new (winston.transports.MongoDB)({
      host: 'localhost',
      db: 'logDb',
      collection: 'log',
      level: 'info',
      levels: customLevels.levels,
      handleExceptions: true
    })
  ]
})

// set the coloring
winston.addColors(customLevels.colors)