Nodejitsu

Save time managing and deploying your node.js app. Code faster with jitsu and npm

Put Your Logs Anywhere with Winston

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

This is Part Two of a two part post on logging in node.js — In any language there are a lot of options when it comes to logging. The issue that we saw at Nodejitsu, was that all of these libraries coupled their storage mechanism of choice (files, databases, etc.) to the API that was exposed to the programmer.

This was the motivation behind winston: a multi-transport logging library for node.js. A transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, one may want error logs to be stored in a persistent remote location (like a database), but all logs output to the console or a local file.


Getting Started with winston

There is a robust amount of documentation for winston on the GitHub project page. To get started you want to install winston using npm:

  npm install winston

Here is a quick summary of basic logging with winston. We'll require winston, configure it with two transports, and log some test data:

var winston = require('winston');

winston.add(winston.transports.Riak, { level: 'warn' });

winston.add(winston.transports.File, { filename: 'mylogfile.log', level: 'silly' });

//
// Logging with no callback ('fire and forget')
//
winston.log('info', '127.0.0.1 - Theres no place like home');

//
// Logging with a callback
//
winston.log('info', '127.0.0.1 - Theres no place like home', function (err, level, msg) {  
  // Do something once you've to all your transports
});

//
// Logging with level (info) specific methods
//
winston.info('127.0.0.1 - Theres no place like home');  


"CHILL WINSTON! ... I put it in the logs"

If you're curious about where the name 'winston' comes from, it's an homage to Lock, Stock, and Two Smoking Barrels. Really curious? Check out the video clip (Definitely safe for work).


Get to the Point Already!

Winston boasts several important features:

  1. Multiple transports configurable by level (console, file, riak, loggly)
  2. [Coming Soon] Configurable Logging Levels. Currently based loosely off npm levels
  3. Optional Logging of Metadata
  4. Easy-to-use profiling of your node.js code
  5. Extensible with Custom Transports
  6. Evented logging with optional callbacks

If any of this has perked your interest, check out the Github Project for more documentation and sample usage.