Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/juanjh1/asimilation/llms.txt

Use this file to discover all available pages before exploring further.

The logger middleware automatically logs all incoming HTTP requests with detailed information including method, URL, status code, and response time.

What It Does

The logger middleware:
  • Captures request start time
  • Logs request details when the response completes
  • Displays color-coded status codes in the console
  • Measures and reports response time in milliseconds
  • Uses the chalk library for colored output

Log Format

Each log entry follows this format:
➤ [YYYY-MM-DD HH:MM:SS] { METHOD /path } -> STATUS_CODE (XXms)

Color Coding

Status codes are color-coded for easy identification:
  • Green (2xx): Successful responses
  • Blue (3xx): Redirects
  • Magenta (4xx): Client errors
  • Red (5xx): Server errors
  • Yellow (1xx): Informational
  • Gray: Unknown status codes

How It Works

The logger middleware is automatically registered with the MiddlewarePipeline when imported. Here’s the implementation:
import { MiddlewarePipeline } from "../../core/middleware-manager.js";
import { ServerResponse } from "http";
import { ArgumentedIncomingMessageInterface } from "../../interfaces/custom-request.js";
import { createLog } from "../../utils/logger.js";
import { timeTakedToResolve } from "../../helpers/date.js";

MiddlewarePipeline.addMiddleware(
    (req, res, next) => {
        const start: Date = new Date();
        
        req.on("end", () => {
            let statusCode: number | undefined = res.statusCode;
            let method: string | undefined = req.method;
            let url: string | undefined = req.url;
            const finalDate: Date = new Date();
            
            const timeTaked: number = timeTakedToResolve(start, finalDate);

            if (statusCode && method && url) {
                createLog(statusCode, method, url, timeTaked);
                return;
            }

            throw new Error("Error in the logger");
        });
        
        next();
    }
);

Key Features

1
Event-Driven Logging
2
The middleware attaches to the request’s end event, ensuring logs are only written after the response is fully sent.
3
Performance Tracking
4
Response time is calculated by comparing the start date with the final date when the request ends:
5
const timeTaked: number = timeTakedToResolve(start, finalDate);
6
Non-Blocking
7
The middleware calls next() immediately, allowing the request to proceed without waiting for the response to complete.

Enabling the Logger

The logger middleware is part of the default middleware stack. To enable it, simply import it in your application:
import "asimilation/default/middleware/logger";
import { asi, url } from "@asimilation/core";

// Define your routes
url.get("/", (req, res) => {
    res.sendJson({ message: "Hello World" });
});

// Start the server
asi.setup(3000, "");
asi.run();

Example Output

When running your application, you’ll see colored logs in the console:
 [2026-03-05 14:23:45] { GET /api/users } -> 200 (45ms)
 [2026-03-05 14:23:46] { POST /api/users } -> 201 (123ms)
 [2026-03-05 14:23:47] { GET /api/users/999 } -> 404 (12ms)
 [2026-03-05 14:23:48] { PUT /api/users/1 } -> 500 (67ms)
The logger middleware runs for all requests and cannot be disabled per-route. If you need conditional logging, consider creating a custom middleware.

Use Cases

  • Development: Monitor API requests in real-time during development
  • Debugging: Identify slow endpoints by checking response times
  • Monitoring: Track request patterns and error rates
  • Performance: Analyze response times to optimize slow routes
For production environments, consider integrating with a structured logging library that supports log levels, file output, and log aggregation services.

Build docs developers (and LLMs) love