Sling Academy
Home/Node.js/ExpressJS: Using MORGAN to Log HTTP Requests

ExpressJS: Using MORGAN to Log HTTP Requests

Last updated: February 06, 2024

Introduction

Logging HTTP requests is a critical part of any web application’s maintenance and monitoring strategy. It enables developers and system administrators to keep track of what’s happening in the application, helping in debugging issues and optimizing performance. In the Node.js ecosystem, ExpressJS is one of the most popular web server frameworks for building web applications. To aid in logging HTTP requests in ExpressJS applications, the Morgan middleware offers a simple yet powerful solution.

This tutorial will guide you through the process of integrating Morgan into your ExpressJS application for effective logging of HTTP requests. From installation to configuration, and understanding the different predefined formats Morgan offers, we’ll cover everything you need to start logging with precision.

Setting Up Your Environment

Before diving into Morgan, make sure you have Node.js and npm (Node Package Manager) installed on your system. These will allow you to set up an ExpressJS project and install Morgan. If you’re new to Node.js or ExpressJS, consider brushing up on the basics before proceeding.

Step 1: Creating an ExpressJS Application

mkdir myExpressApp
cd myExpressApp
npm init -y
npm install express

This set of commands creates a basic ExpressJS application. The npm init -y command initializes a new Node.js project with default settings, and npm install express adds ExpressJS to the project.

Step 2: Installing Morgan

npm install morgan

With your ExpressJS application in place, install Morgan by running the above command. This will add Morgan to your project’s dependencies, making it available for use in your application.

Integrating Morgan into ExpressJS

Integrating Morgan into your ExpressJS application is straightforward. First, include Morgan in your main application file (usually app.js or server.js) using a require statement. Then, use app.use() to tell Express to use Morgan as a middleware.

const express = require('express');
const morgan = require('morgan');

const app = express();

// Use Morgan middleware
app.use(morgan('combined'));

This code snippet sets up your Express application to use Morgan with the ‘combined’ format, which is a predefined log format in Morgan, providing detailed information about each request.

Understanding Morgan’s Predefined Formats

Morgan comes with several predefined formats that determine the level of detail in your logs. Some of the most commonly used formats include:

  • tiny: Minimal output, focusing on the method, URL, response status, and response time.
  • combined: Apache combined log format. Includes detailed information like the remote address, request method, URL, HTTP version, status code, response size, and referrer.
  • dev: Concise output during development. Includes the request method, URL, status code, response time, and the length of the response body.

To use a different format, simply replace ‘combined’ in the app.use(morgan('combined')); statement with your format of choice.

Customizing Morgan

Beyond the predefined formats, Morgan offers the ability to create custom logging formats. This feature is especially useful for tailoring the logs to meet specific requirements of your application.

app.use(morgan(function (tokens, req, res) {
  return [
    tokens.method(req, res),
    tokens.url(req, res),
    tokens.status(req, res),
    tokens.res(req, res, 'content-length'), '-'
  ].join(' ');
}));

This custom logging function uses tokens to build a log string that includes the request method, URL, status code, and content length. You can adjust this function by adding or removing tokens to match your logging needs.

Logging to Files

In production environments, you may want to save logs to files rather than printing them to the console. Morgan, in combination with additional Node.js modules like ‘fs’ and ‘path’, can be configured to write logs to a file.

const fs = require('fs');
const path = require('path');
const morgan = require('morgan');
const rfs = require('rotating-file-stream'); // For log rotation

const app = express();

// create a write stream (in append mode)
var accessLogStream = rfs.createStream('access.log', {
  interval: '1d', // rotate daily
  path: path.join(__dirname, 'log')
});

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}));

This configuration uses rfs.createStream to create a rotating file stream, which will write logs to ‘access.log’ within a ‘log’ directory under your project, rotating the file daily. It provides a systematic way to manage log files and prevent them from growing excessively large.

Conclusion

Logging is an indispensable part of web application development and maintenance. Morgan middleware offers an elegant and streamlined solution for logging HTTP requests in the ExpressJS applications. From predefined formats to custom logging functions, Morgan provides the flexibility and utility needed to monitor and optimize your applications effectively. With the guidelines provided in this tutorial, you’re now equipped to integrate and configure Morgan in your ExpressJS projects, ensuring robust and detailed logging of HTTP requests.

Next Article: Using ExpressJS and Multer with TypeScript

Previous Article: How to generate Excel (xlsx) files using Node.js

Series: Node.js & Express Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates
  • ExpressJS: How to pass variables to Pug templates