Setting Up a Proper Logger in Sails.js

I'm a big fan of the Node framework Sails.js. It's based on Express, but gives you all of the great features to help you be more productive that Rails developers love, and take for granted every day. However, I'm going to come right out and say that I'm not a fan of their default logger.

The good news is that this is easy to fix.

The logger bothers me for a few a few reasons. The biggest offense is that it doesn't have timestamps. I don't know what you normally use logs for, but if I'm looking at log files, its usually because something bad happened, and being about to isolate a time frame, or even find the request in the mess of logs, a timestamp is really useful.

Sails makes modifying the logger really simple, so you can easily insert your own timestamped request logger, which I'll show you how to do below.

First we're going to need to install a new logging module. I'd recommend using Winston for this. It's a great logger that's been production tested.

npm install winston --save

Next, open up the config.log.js file. Now that you've installed Winston, you can require it, and include the modified logger as shown.

var winston = require('winston');

module.exports.log = {
  level:'info',
  'colors': false,
  'custom': new (winston.Logger)({
    'transports': [
      new (winston.transports.Console)({
        'level': 'info',
        'colorize': true,
        'timestamp': true,
      }),
    ]
  })
};

There are other options you can pass it, and for that you can check the docs, but now, when lifting your Sails app you should have a timestamp on each line logged.

2015-09-26T16:50:38.805Z - info: