In this post we will be discussing about better logging in Node.js. If you are writing an application, detailed logging is required for spotting problems and debugging. Logging is an integral part of any application. A well implemented logging mechanism in the application reduces the time to identify the root cause of the problem.
But developer should be careful on how much amount of logging should be done, thus in this post we will be going through some of the npm packages which will be helpful in reducing the amount consumed by the developer to do logging.
The most basic kind of logging used in Node.js is to log the errors or information using the built in console. Console module can put the messages as an info or as an error as shown below :
But it definitely does not serve our purpose for better logging because it lacks the feature of time-stamps, we cannot distinguish between the info and error and also the severity of the error cannot be distinguish.
There are number of modules which can help us in doing the logging right for our application. Here we will be discussing about Winston and Bunyan.
Bunyan: As per Bunyan, it is a simple and fast JSON logging library for nodejs services. They also have a Bunyan CLI tool for nicely viewing the logs.
How to Use Bunyan
Create a file named example5.js whose contents will be :
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myFirstBunyanExample'});
log.info('1st Log using Bunyan');
log.warn('It's just a warning');
And then run it as node example5.js | bunyan
Winston: As per Winston, it is a multi-transport async logging library for node.js. Winston is designed to be a simple and universal logging library with support for multiple transports.
How to use Winston
Create a file name example6.js whose contents will be "
var winston = require('winston');
winston.log('info', 'Coding Defined Info Logger at www.CodingDefined.com');
winston.info('Coding Defined Again Info Logger at www.CodingDefined.com');
And then run it as node example6.js
Winston and Bunyan are both established logging frameworks and have lot of features. It all depends on the preference and how easy it to integrate in your application.
Please Like and Share the Blog, if you find it interesting and helpful.
But developer should be careful on how much amount of logging should be done, thus in this post we will be going through some of the npm packages which will be helpful in reducing the amount consumed by the developer to do logging.
Simple Logging Using Console
The most basic kind of logging used in Node.js is to log the errors or information using the built in console. Console module can put the messages as an info or as an error as shown below :
But it definitely does not serve our purpose for better logging because it lacks the feature of time-stamps, we cannot distinguish between the info and error and also the severity of the error cannot be distinguish.
Logging using NPM Modules
There are number of modules which can help us in doing the logging right for our application. Here we will be discussing about Winston and Bunyan.
Bunyan: As per Bunyan, it is a simple and fast JSON logging library for nodejs services. They also have a Bunyan CLI tool for nicely viewing the logs.
How to Use Bunyan
Create a file named example5.js whose contents will be :
var bunyan = require('bunyan');
var log = bunyan.createLogger({name: 'myFirstBunyanExample'});
log.info('1st Log using Bunyan');
log.warn('It's just a warning');
And then run it as node example5.js | bunyan
Winston: As per Winston, it is a multi-transport async logging library for node.js. Winston is designed to be a simple and universal logging library with support for multiple transports.
How to use Winston
Create a file name example6.js whose contents will be "
var winston = require('winston');
winston.log('info', 'Coding Defined Info Logger at www.CodingDefined.com');
winston.info('Coding Defined Again Info Logger at www.CodingDefined.com');
And then run it as node example6.js
Which one to use : Winston or Bunyan ?
Winston and Bunyan are both established logging frameworks and have lot of features. It all depends on the preference and how easy it to integrate in your application.
Please Like and Share the Blog, if you find it interesting and helpful.
No comments:
Post a Comment