Logging in NodeJS using Papertrail

Gaurav Umrani
codeburst
Published in
3 min readAug 14, 2018

--

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.

Step 1

First, we need to install the required modules
npm install winston@2.4.3 express-winston winston-papertrail —- save

winston is a logging library with support for multiple transports, in winston a 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 winston

Step 2

So, after installing modules, create a file logger.js and paste this code

Step 3

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’);

Step 6

Now we will create an 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

  1. 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)
  2. Now hit Create Alert button which is at bottom right side
  3. Now you will see lots of integration methods email etc, we will choose slack for now
  4. 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 )
  5. 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!

--

--