express-requests-logger is an Express middleware designed for comprehensive logging of incoming HTTP requests and outgoing responses within Node.js applications. The current stable version is 4.0.3, with v4.0.1 released in August 2024, indicating active maintenance though its release cadence can be somewhat irregular. A key differentiator is its extensive configuration options, allowing developers to precisely control what data is logged. This includes masking or excluding sensitive fields from request and response bodies, headers, and query parameters. It also supports integration with custom loggers, although it's primarily tested with Bunyan, and offers a `shouldSkipAuditFunc` for dynamic conditional logging. The `doubleAudit` feature provides flexibility for logging requests both upon arrival and after the response is sent, which can be crucial for debugging systems prone to crashes during request processing.
npm install express-requests-loggerVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic Express application with `express-requests-logger` to demonstrate request and response logging. It integrates with Bunyan, enables JSON body parsing, configures sensitive data masking for both request and response bodies, excludes specific headers, and utilizes URL exclusion for health check endpoints. It also includes an example of `shouldSkipAuditFunc` for conditional logging.
Review your logging system's configuration and alerting rules. If you have specific alerts or filters based on log levels, ensure they correctly capture 'ERROR' level messages for 5xx status codes to avoid missing critical alerts. No code changes are typically required in your application.
Upgrade your Node.js runtime environment to version 16 or higher. Node.js 16 is the minimum officially supported version for `express-requests-logger` v4.0.0 and subsequent releases.
If not using Bunyan, ensure your custom logger provides an `info` method with the signature `(data: object) => void`. You might need to create a simple wrapper function to adapt your logger's API to this expected format.
Be mindful of log volume when using `doubleAudit`. Configure your log aggregation and analysis tools to handle or de-duplicate these entries if necessary. If you only need logging after the full request-response cycle, set `doubleAudit: false`.
For ES Modules (ESM), use `import audit from 'express-requests-logger';`. For CommonJS, use `const audit = require('express-requests-logger');`.Ensure that the logger object passed to the `logger` option is correctly initialized and exposes an `info` method, as demonstrated with `bunyan.createLogger()` in the quickstart example.
Ensure that your routes and other middleware do not attempt to send responses or modify headers after `res.send()`, `res.json()`, or `res.end()` has already been called. Always return or use `next()` after sending a response if subsequent middleware should not execute.