Express Request/Response Logger Middleware
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
-
TypeError: expressLogging is not a function
cause This error typically occurs when attempting to use the `expressLogging` function directly without passing a logger instance, or if importing it incorrectly in an ESM environment.fixEnsure you are calling `expressLogging` with a logger argument (e.g., `app.use(expressLogging(logger));`) and that you are using `const expressLogging = require('express-logging');` in a CommonJS context. -
TypeError: logger.info is not a function
cause The `express-logging` middleware expects the provided logger object to have an `info` method that accepts string formatting or a params object.fixVerify that the logger object you are passing to `expressLogging` (e.g., `logger`) has a callable `info` method with the expected signature. For instance, `logops` provides this by default.
Warnings
- breaking This package is no longer maintained and has not been updated in over eight years. It is highly likely to be incompatible with modern versions of Node.js (e.g., Node.js 14+) and Express (e.g., Express 5). Using it may lead to unexpected errors or security vulnerabilities.
- gotcha The package explicitly targets older Node.js versions (engines: `>= 0.10.26`). It was last updated to fix compatibility with 'old node releases,' implying it was not designed with current Node.js features or performance characteristics in mind.
- gotcha The middleware is CommonJS-only. Attempting to use `import expressLogging from 'express-logging';` in an ES module context will fail with a `TypeError: expressLogging is not a function` or a similar module resolution error.
Install
-
npm install express-logging -
yarn add express-logging -
pnpm add express-logging
Imports
- expressLogging
import expressLogging from 'express-logging';
const expressLogging = require('express-logging'); - logger
import logger from 'logops';
const logger = require('logops'); // Or your preferred logger - app.use
app.use(expressLogging); // Missing logger argument
app.use(expressLogging(logger, options));
Quickstart
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}`);
});