-1

I recently stumbled upon winston logging and started to use in one of my nodejs applications.

My configuration is below:

// Setup winston logging
var loggerSettings = {
  level: logConfig.dev.level,
  //format: combine(label({label: 'authServer'}), timestamp(), myFormat),
  levels: logLevels.levels,
  transports: [ new winston.transports.Console({
      colorize:true,
      format: winston.format.simple(),
      json: false
    }) 
  ]  // by default console log will always be present.
};

winston.addColors(logLevels);
logger = winston.createLogger(loggerSettings);
module.exports = logger;

And.. logLevels is defined as below...

const logLevels  = {
  levels: {
    fatal: 0,
    error: 1,
    warning: 2,
    info: 3,
    debug: 4,
    trace: 5
  },
  colors: {
    fatal:    'red',
    error:    'orange',
    warning:  'yellow',
    info:     'green',
    debug:    'blue',
    trace:    'gray'
  }
};

Even though I declared the colors option (colorize:true), I am unable to see any colored output on the console. I have also required the colors modules in the beginning of this file.

Screenshot of log output without colors

Thanks for any help, in fixing this issue.

Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78

1 Answers1

0

I'm not sure if this is 100% correct, but it works for me very nicely. I was getting the same behaviour, e.g. colors not showing correctly. A bit of moving things around and it works:

var winston = require('winston');

const logLevels  = {
  levels: {
    fatal: 0,
    error: 1,
    warning: 2,
    info: 3,
    debug: 4,
    trace: 5
  },
  colors: {
    fatal:    'red',
    error:    'orange',
    warning:  'yellow',
    info:     'green',
    debug:    'blue',
    trace:    'gray'
  }
};

var logger = new (winston.Logger)(logLevels);

logger.add(winston.transports.Console, {
    level: logConfig.dev.level,
    json: false,
    colorize: true
});

module.exports = logger;

var testLevels = ['trace', 'debug', 'info', 'warning', 'fatal'];
testLevels.forEach( (testLevel) => { 
    logger.log(testLevel, `My ${testLevel} test`);
});

I use Winston quite a bit and I find it's a really nice module!

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40