Tag Archives: Node

Node.js logger: winston

Winston is a fancy and flexible logging library developed by the guys from Nodejistu. It is totally decoupled allowing an easy extension and customization for any Node.js project.
In a configuration file I created a simple data structure log with the different levels of logging and respective colorization. Afterwards it is trivial to instantiate my own logger adding the so call “multi transport” supported by Winston which enables different storage devices (console, file, mail, etc). Finally I set the logging levels/colors previously defined, and I’m ready to go!
As aforementioned, Winston is highly flexible and supports many tweaks which are all thoroughly detailed in github.

var log = {
    'logger' : {
      'levels': {
        'detail': 0,
        'trace': 1,
        'debug': 2,
        'enter': 3,
        'info': 4,
        'warn': 5,
        'error': 6
      },
      'colors': {
        'detail': 'grey',
        'trace': 'white',
        'debug': 'blue',
        'enter': 'inverse',
        'info': 'green',
        'warn': 'yellow',
        'error': 'red'
      },
    }
  };

  function getLogger() {
    var logger = new (winston.Logger)({
      'transports': [
      new (winston.transports.Console)(
      {
        'level': 'enter',
        'colorize': true
      }),
      new (winston.transports.File)(
      {
        'filename': 'logging-file.log'
      })]
    });

    logger.setLevels(log.logger.levels);
    winston.addColors(log.logger.colors);

    return logger;
  }
Advertisements
Tagged , , , ,
Advertisements