Wynn Netherland changelog.com/posts

winston: Pluggable async logging library for Node.js

The Nodejitsu team has released Winston, a pluggable, async logger for Node.js that also supports multiple transports. Out of the box, Winston includes several transports including:

  • Console: Output to the terminal
  • Filesystem: Append to a file
  • Riak: Log to a remote Riak server, featured on Episodes #14 and #40
  • Loggly: Log it to the cloud with Loggly, a logging SAAS

Using Winston

Winston can be installed via npm

npm install winston

In the simplest case, we can log to the console with just a few lines of code:

var winston = require('winston');

winston.log('info', 'Hello from Winston!');
winston.info('This also works');

You’re also free to set up your own loggers and choose your transport:

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: 'somefile.log' })
  ]
});

We also mentioned that Winston supports async, Node.js callback-style logging.

logger.on('log', function (transport, level, msg, meta) {
  // [msg] and [meta] have now been logged at [level] to [transport]
});

logger.on('error', function (err) {
  // handle an error
});

logger.info('CHILL WINSTON!', { seriously: true });

One of the more interesting features of Winston is its support for logging metadata with log events. Depending on your transport, metadata is either simply displayed or stored alongside your log events. Perhaps the most robust example is storing metadata as a JSON literal in your Riak logging store:

winston.log('info', 'Test Log Message', { anything: 'This is metadata' });

For more on Winston or to find out how to support for your favorite transport, check the source. For a bit of background on the project name, check out Charlie’s blog post.

[Source on GitHub] [Blog post]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00