Logging in NodeJS Using Papertrail

Engineering Aug 07, 2018

Recently, I was implementing a 3rd party service in our project and was facing some issues in implementing, I wanted to check what’s missing, what data is being sent and received.

Advantages of Papertrail

I was looking for a logger that shows all my requests at a single place and also gives alerts whenever an error occurs in my application, so after spending some time on the internet, I found Papertrail very useful for my needs.

Why Papertrail Is The Best Logger?

There are numerous reasons why I chose Papertrail. Although console.log() somehow helps in my mentioned problem, but it’s not a good solution for production, it becomes difficult if you want to see the last week logs.                      

Also I wanted to handle application errors too. I needed a good log management that helps me in solving these issues. So, I ended up choosing Papertrail.

Along with log management, Papertrail provides a lot of other useful tools as well text log , real time events and alerts etc.                                                                                                  
Let’s first go to the basics and then hop to the main steps for integration of NodeJS in papertrail.

What Exactly Is NodeJS?

NodeJS is a program created on Chrome's Javascript runtime built primarily for creating scalable network applications. It is lightweight, efficient and works perfectly for real-time applications that run across various devices.

What Is Papertrail?

Papertrail is a non complex powerful log management designed and created by engineers that provides real time log tailing and supports mostly every log type. It untangles troubleshooting and reduce Mean Time to Repair (MTTR)

What Is Logging in NodeJS?

To complete an application life cycle, logging is important. Starting from creation, ending at debugging or in experimenting with new features, logs support us in every part of the process. It simplifies the    

  1. Process of analyzing the data        
  2. Resolving bugs            
  3. Early detection of problems                    

What is NodeJS mostly used for?

NodeJS has a number of applications. It is primarily used in:

  1. Non Blocking
  2. Event-driven servers
  3. Back end API services

Steps to Integrate Papertrail to NodeJS Application


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 winston.


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

const winston = require('winston');

var logger = new winston.transports.Papertrail({
    host: 'logs7.papertrailapp.com', // you get this from papertrail account
    port: 12345, //you get this from papertrail account
    colorize: true,
    handleExceptions: true
module.exports = logger;


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

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const logger = require('./logger'); //our logger module
const expressWinston=require('express-winston'); // it will help in using logger as middleware

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.use('/public', express.static(path.join(__dirname, '../public')));
app.set('view engine', 'ejs');

app.use(expressWinston.logger({ // use logger to log every requests
  transports: [logger],
  meta: false, // optional: control whether you want to log the meta data about the request (default to true)
  msg: `{{req.ip}} - {{res.statusCode}} - {{req.method}} - {{res.responseTime}}ms - {{req.url}} - {{req.headers['user-agent']}}`, // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
  expressFormat: false, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
  colorize: true

app.use('/api', require('./controllers/user/index.js'));
app.listen(process.env.PORT || 5000, () => {

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 use this.

Step 4

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 ,we will choose slack for now
  • On next page, add details on 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.

What’s the biggest thing you’re struggling with right now that we as a technology consulting company can help you with? Feel free to reach out to us at info@jalantechnologies.com. We hope our assistance will help!


Gaurav Umrani

Product Engineer | JavaScript | Angular | Node | React | Go

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.