Introducing async-request-logger — A Simple Request-Level Logger for Node.js

I’m excited to share a small but powerful npm package I recently published: async-request-logger.

🔗 GitHub: github.com/chaitanyakrishna26399/async-request-logger

📦 NPM: npmjs.com/package/async-request-logger

If you’ve ever tried debugging a Node.js application with multiple services or deeply nested async calls, you know the pain:

You see an error log, but you have no idea which request caused it.

This problem becomes even bigger in real production systems, where hundreds of requests hit the server simultaneously. That’s exactly what inspired me to build this library.


🧩 The Problem: Tracking Logs Across an Entire Request

In a typical Node.js backend:

  • Controllers log incoming requests
  • Services log business logic
  • Database layers log queries
  • Error handlers log failures

But without a requestId, all logs look the same.

So when an error happens, you can’t trace its journey:

❌ Which user triggered it? ❌ Which API route did it start from? ❌ What other logs belong to the same request?

Manually passing requestId everywhere is messy and error-prone. Existing libraries are often too complex or not beginner-friendly.


🛠️ The Solution: Async Local Storage

async-request-logger uses Node’s AsyncLocalStorage to automatically create and store a unique requestId for every incoming API call.

The best part?

👉 All logs inside the same async chain automatically include the same requestId

No need to manually pass it around.


✨ Key Features

✔️ Generates a unique requestId for each request

✔️ Logs are automatically tagged, even inside nested async calls

✔️ Works out-of-the-box with Express.js

✔️ Zero configuration required

✔️ Helps debug production issues much faster


How Developers Can Use It

1️⃣ Install

npm install async-request-logger
        

2️⃣ Set up middleware

const { setupAsyncRequestLogger } = require("async-request-logger");

app.use(setupAsyncRequestLogger());
        

3️⃣ Log anywhere in your code

const { logger } = require("async-request-logger");

logger.info("User requested profile");
logger.error("Failed to fetch user");
        

Output example:

[requestId: 9f3d12ac] User requested profile
[requestId: 9f3d12ac] Failed to fetch user
        

The same requestId flows automatically — even inside deep service layers.


Real Use Case

Imagine you have:

  • A controller
  • A service handling business logic
  • A repository accessing the database

All three logs will now include the same requestId.

So debugging becomes like reading a timeline of a single request, instead of random scattered logs.


Why I Built This Package

I needed a clean logging solution that:

  • Works with async/await
  • Does not require boilerplate
  • Helps track a request end-to-end
  • Is simple enough for beginners
  • Works seamlessly in microservices


Shubham Singh

ZöTok.AI545 followers

4mo

Insightful. CFBR

Like
Reply

Well done Chaitanya

Like
Reply
Mayur Athavale

ZöTok.AI365 followers

4mo

Great package !

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories