Express Request/Response Logger Middleware

1.1.1 · abandoned · verified Wed Apr 22

express-logging is an Express middleware designed to log incoming requests and outgoing responses. The current stable version is 1.1.1, released over eight years ago, indicating the project is no longer actively maintained. It allows for configurable logging using any logger that exposes an `info` method, such as `logops`, and supports blacklisting specific URL paths to prevent logging. A key differentiator is its 'policy' option, which enables logging in either a single 'message' string format or a 'params' object format, making it suitable for integration with log processing systems like ELK stack. It also automatically calculates and logs the duration of each request-response cycle. Given its age, it is likely incompatible with modern versions of Node.js and Express.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `express-logging` with a standard logger and an example of configuring the middleware with custom blacklist paths and a 'params' logging policy.

const express = require('express');
const expressLogging = require('express-logging');
const logger = require('logops'); // Example logger

const app = express();

// Basic usage: Log all requests/responses
app.use(expressLogging(logger));

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

// Example with custom options: blacklist '/images' and use 'params' policy
const blacklistPaths = ['/images', '/html'];
app.use('/api', expressLogging(logger, { blacklist: blacklistPaths, policy: 'params' }));

app.get('/images/test.png', (req, res) => {
  res.send('This image access should not be logged by the second middleware.');
});

app.get('/api/data', (req, res) => {
  res.json({ message: 'API data' });
});

const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

view raw JSON →