Easy JSON logging in Node JS using Woodlot
There are a lot of options available when it comes to choosing a tool for data logging in your Node JS application. At times you would find yourself switching back and forth between different tools just to get that perfect logger configuration set up properly for your project.
A common need while developing applications is to have your data logs get aggregated in an output file, instead of just getting logged to the console or the standard output stream (stdout). This makes it easy to resolve bugs faster, gain insights from the data being logged and maybe analyse it at a later stage.
For this very purpose we are required to use third-party data logging modules, and we'll be looking at one such open source module called Woodlot.
What is Woodlot?
Woodlot is an all-in-one JSON logging module for Node JS which was open sourced by AdPushup Inc. It supports -
- HTTP middleware logging for Express JS, Restify etc.
- Custom logging with different logging levels.
- Log output in JSON format with request body/query params, headers and cookies.
- Apache common and combined log formats as output.
- Multiple file streams for log aggregation.
- A simple to use events API.
- Node JS version 0.10 and above.
The best part about using Woodlot is that you can use it to integrate middleware logging and custom logging both in your application, instead of installing separate modules.
Woodlot logs JSON output by default which makes it easier to aggregate and understand the logged data.
The Woodlot middlewareLogger can be hooked into an existing Express JS middleware chain and can be used to log all HTTP requests.
This middleware logger configuration will generate log output in JSON format.
The Woodlot middleware logger supports whitelisting and strict checking for routes and user analytics (platform, country) logging as well. Apart from JSON format support, it also also has support for logging in Apache's access log formats.
Documentation on all of the different options that the middleware logger supports can be found here.
The Woodlot customLogger can be used to perform custom logging with different logging levels.
This custom logger configuration will generate log output in JSON format.
Documentation on all of the different options that the custom logger supports can be found here.
Woodlot emits events at various operations that can be used to track critical data. This events API is what makes Woodlot truly extensible.
These events can be captured and can be sent to a third party service or to a database for aggregation and analysis.
There are various middleware logger and custom logger events that Woodlot emits. Documentation of the Woodlot events API can be found here.
If you or your company use Woodlot or any of my other projects, or like what I'm doing, please consider backing me so I can continue maintaining and working on these projects and new ones. Your pledge could be as small as $1/month. I'd really appreciate your support! To become a patron kindly click this button -Become a Patron!