Logging requests in Express app

June 16, 2024

While working on an Express.js application recently, I faced the need to log all requests passing through it. For years, I've been a loyal user of morgan, a popular HTTP request middleware logger for Node.js.

However, morgan hasn't seen any updates in the past four years. It was high time for a change! So, I decided to try out express-requests-logger, a more current logging middleware.

Setting Up express-requests-logger

Getting express-requests-logger up and running is straightforward. Here's how I did it:

npm install express-requests-logger
npm install --save-dev @types/express-requests-logger

Next, I integrated it into my application:

import audit from "express-requests-logger";
// Other imports and app setup code
app.use(audit());

Why express-requests-logger?

Double Audit Feature

One feature I absolutely love is doubleAudit. This logs incoming requests and their corresponding responses. It's incredibly useful for monitoring the full lifecycle of requests, especially when debugging complex interactions.

Efficient Endpoint Management with excludeURLs

With frequent health check calls from Kubernetes on certain endpoints, my logs were getting cluttered. The excludeURLs feature came to the rescue, allowing me to exclude these endpoints from logging and keep my log output clean and relevant.

Enhanced Security with Flexible Masking and Exclusion Options

Express-requests-logger offers several options for enhancing security and privacy in logs:

  • excludeBody / maskBody: Great for omitting or anonymizing sensitive data in request bodies.
  • maskQuery: Helps mask data in query parameters.
  • excludeHeaders / maskHeaders: Useful for handling sensitive header information.

These features are crucial for maintaining compliance with data protection regulations and ensuring that sensitive information stays secure.

Custom Logger Integration

The flexibility to integrate a custom logger is a win for me. I use winston for logging because of its versatility and performance. Express-requests-logger allows me to continue using winston seamlessly, which keeps my setup consistent and efficient.

I highly recommend giving express-requests-logger a try!