HTTP Request Logger Middleware

raw JSON →
3.8.0 verified Thu Apr 23 auth: no javascript abandoned

logger-request is an HTTP request logging middleware for Node.js, primarily designed for Express applications. It leverages the Winston logging library (specifically Winston v2.3.1 as of its last release, 3.8.0) to provide flexible output options including file logging, console output, and integration with other Winston transports like MongoDB. Key features include daily log rotation, customizable log fields such as process ID, referrer, authentication status, transfer rates, and user agent. The package reached its latest version, 3.8.0, in 2017, and has not received updates since, making it incompatible with modern Node.js versions and the significantly evolved Winston v3+ ecosystem. Its release cadence was active in the mid-2010s but has ceased, rendering it effectively unmaintained.

error TypeError: logger.transports.Console is not a constructor
cause This error typically occurs when `logger-request` (which depends on Winston v2) is run in an environment where Winston v3 or later is also present or mistakenly assumed. Winston v3 changed its transport architecture significantly.
fix
Ensure that your node_modules only contains winston@2.x if you intend to use logger-request. If your project requires Winston v3+, you must migrate away from logger-request.
error Error: Cannot find module 'winston'
cause `winston` is a peer dependency (or strong runtime dependency) of `logger-request` and must be installed explicitly in your project.
fix
Install winston: npm install winston@2 (to match the version logger-request expects) or npm install winston if you are using an older npm that resolves peer dependencies automatically.
error TypeError: app.use requires middleware functions but got a [object Undefined]
cause This error can occur if `require('logger-request')` returns `undefined`, which might happen if the package's main file is not found or has an error during loading.
fix
Verify that logger-request is correctly installed via npm install logger-request. Check for any installation errors or conflicts in your node_modules directory.
breaking The package is hardcoded to use Winston v2.3.1. It is not compatible with Winston v3.x or later due to significant API changes in Winston's major versions. Attempting to use a newer Winston version will result in errors.
fix Downgrade Winston to a compatible v2.x version if you must use this package, or migrate to a modern logging solution that supports Winston v3+.
breaking The package has not been updated since 2017 and has a Node.js engine requirement of `>=4`. It is not officially tested or supported on modern Node.js versions and may encounter runtime issues or security vulnerabilities.
fix Consider migrating to a currently maintained HTTP request logger middleware for modern Node.js environments.
gotcha The `deprecated` option within the `logger(options)` object does not indicate a deprecation of the package itself or a feature. Instead, it's a flag (default `false`) that alters the timing of log writing from the default `listener` to after `res.end()` (if `true`). This naming can be confusing.
fix Understand that `options.deprecated` controls log timing, not feature deprecation. Use `false` for default behavior (logging on listener) or `true` for logging after response ends.
npm install logger-request
yarn add logger-request
pnpm add logger-request

Demonstrates basic Express integration, setting up the middleware to log requests to both a daily rotating file (`app.log`) and the console with custom fields.

const express = require('express');
const logger = require('logger-request');
const app = express();

// Configure the logger to write to 'app.log' and also display to console
app.use(logger({
  filename: 'app.log',
  console: true,
  daily: true, // Rotate log file daily
  winston: {
    level: 'info',
    json: true,
    timestamp: true
  },
  custom: {
    pid: true, // Log process ID
    referer: true, // Log HTTP referer header
    agent: true // Log user-agent header
  }
}));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});