Add some sweet logging to your Javascript/Node.js apps with Winston

March 19th, 2013 by admin Leave a reply »

If you are looking for a logging solution to your javascript or node.js apps, look no further, enter Winston.  This little tool can write logs from your apps to a file, to the console, or to a database and you can even query the logs with its own built in syntax, how sweet is that?  Anyways, I have included below the code snippets that is needed to get it going, check it out:

//by default, Winston is set to log to console 
var winston = require('winston');

 winston.log('info', 'Hello distributed log files!');
 winston.info('Hello again distributed logs');

//you can change that behavior by doing something like this:
winston.add(winston.transports.File, { filename: 'somefile.log' });
winston.remove(winston.transports.Console);

Here is the github repo for further reading if you are considering this tool https://github.com/flatiron/winston

Pretty simple, just three lines of code and you are logging, just slap a winston.info() anywhere you want to see some info and you are good to go, enjoy!!

Advertisement

Leave a Reply