WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Module requestLogger

    HTTP request logging middleware for WayrApp Backend API

    This module provides comprehensive HTTP request and response logging capabilities for the WayrApp language learning platform backend. It serves as a critical observability component that tracks all incoming HTTP requests and their corresponding responses, providing essential data for monitoring, debugging, performance analysis, and security auditing across the entire application.

    The request logger operates as Express middleware that automatically captures detailed information about each HTTP transaction, including request metadata (method, URL, IP address, user agent), response details (status code, processing duration), and timing information. This data is essential for understanding application behavior, identifying performance bottlenecks, detecting security threats, and maintaining operational visibility in production environments.

    The middleware integrates seamlessly with the Winston-based logging infrastructure, supporting both development and production logging scenarios. In development, it provides colorized console output for immediate feedback, while in production it generates structured JSON logs suitable for log aggregation systems, monitoring dashboards, and automated alerting systems.

    Key architectural features include non-blocking logging operations that don't impact request performance, automatic request duration calculation for performance monitoring, comprehensive request context capture for debugging, and seamless integration with the application's distributed logging infrastructure. The logger is designed to scale with the application's evolution toward a distributed architecture, providing consistent logging behavior across multiple nodes and services.

    Security and privacy considerations include careful handling of sensitive data in logs, automatic IP address logging for security monitoring, user agent tracking for threat detection, and structured logging formats that support security information and event management (SIEM) systems. The logger avoids capturing sensitive request body data while maintaining sufficient context for effective monitoring and debugging.

    Exequiel Trujillo

    1.0.0

    // Basic usage in Express application
    import { requestLogger } from '@/shared/middleware/requestLogger';
    import express from 'express';

    const app = express();

    // Apply request logging to all routes
    app.use(requestLogger);

    // All subsequent routes will be automatically logged
    app.get('/api/users', userController.list);
    app.post('/api/auth/login', authController.login);
    // Integration with other middleware (recommended order)
    import { requestLogger, errorHandler } from '@/shared/middleware';

    const app = express();

    // Security and parsing middleware first
    app.use(helmet());
    app.use(cors());
    app.use(express.json());

    // Request logging after parsing but before routes
    app.use(requestLogger);

    // Application routes
    app.use('/api', apiRoutes);

    // Error handling last
    app.use(errorHandler);
    // Sample log output for incoming request
    // {
    // "level": "http",
    // "message": "Incoming request",
    // "method": "POST",
    // "url": "/api/v1/auth/login",
    // "ip": "192.168.1.100",
    // "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
    // "timestamp": "2024-01-20T10:30:00.000Z"
    // }
    // Sample log output for completed request
    // {
    // "level": "http",
    // "message": "Request completed",
    // "method": "POST",
    // "url": "/api/v1/auth/login",
    // "statusCode": 200,
    // "duration": "145ms",
    // "ip": "192.168.1.100",
    // "timestamp": "2024-01-20T10:30:00.145Z"
    // }

    Functions

    requestLogger