Recently, I was implementing a 3rd party service in our project and was facing some issue in implementing, I wanted to check what’s missing, what data is being sent and received.
console.log() somehow helps in this, but it’s not a good solution for production, it becomes difficult if you want to see the last week logs, also I wanted handle application errors too. I needed a good log management that helps me in solving these issues.
I was looking for a logger that shows all my requests at a single place and also give alerts whenever an error occurred in my application, so after spending some time on the internet, I found Papertrail very useful for my need.
So along with log management, Papertrail provides a lot of other useful tools like text log, Real-time events, Alerts etc.
We’ll now integrate Papertrail to our NodeJS Application, So let’s get started.
First, we need to install the required modules
npm install winston express-winston winston-papertrail —- save
winston is a logging library with support for multiple transports, in
winstona transport is essentially a storage device for your logs, you can read more about it here
express-winston is a middleware for request and error logging of your ExpressJS application
winston-papertrail is Papertrail transport for
So, After installing modules, create a file
logger.js and paste this code
Now in your
app.js file, use the following code. The
expressWinston will add our logger as a middleware, after this, you can see all your request in Papertrail account
Here’s an example of how requests are shown in Papertrail
also if you want to use this as the alternative of console.log, you can you this
logger.log(‘info’,’some demo message’);
Now we will create a Papertrail Alert which will notify us whenever an error occurred, we will be receiving notification in our slack channel, also there are other available options which you can use
- Enter a search string in the search bar, we are looking for errors only, so will write error and hit enter, you can see all errors now (if any)
- Now hit Create Alert button which is at bottom right side
- Now you will see lots of integration methods email etc, we will choose slack for now
- On next page, add details like how you want error reports and at the bottom add slack Integration’s Webhook URL (you can create a new integration to Papertrail and obtain a new webhook URL )
- And hit Create Alert, Now you will be receiving Error notifications to your slack whenever an error has occurred
I hope this approach helps you to integrate log management into your app. Please leave comments or suggestions for improvements!