Save time managing and deploying your node.js app

Nodejitsu has joined GoDaddy

We are excited to join GoDaddy to help spearhead their largest Node.js product: Website Builder. Please read our blog post to learn more about customer transition plans and our continued support for Open Source within the node community.

Want to know all the details?
Read the full blog post or read the GoDaddy Announcement.

Put Your Logs Anywhere with Winston

About the author

nodejitsu nodejitsu

Other popular posts

- Scaling Isomorphic Javascript Code - Keep a node.js server up with Forever - Package.json dependencies done right
- npm cheat sheet - 6 Must Have Node.js Modules

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', ' - Theres no place like home');

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

// Logging with level (info) specific methods
//' - 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.