winston-express-middleware
raw JSON → 0.1.0 verified Sat Apr 25 auth: no javascript maintenance
Express.js middleware for request and error logging using winston. Version 0.1.0 (stable, but low release cadence). Supports whitelisting of request/response properties, custom log levels per status code, and multiple transports. A fork of express-winston with updated whitelisting and winston compatibility. Limited to Node >=0.6.0; no TypeScript definitions. Not actively maintained.
Common errors
error Cannot find module 'winston-express-middleware' ↓
cause Package not installed.
fix
Run 'npm install winston-express-middleware' in your project directory.
error TypeError: expressWinston.logger is not a function ↓
cause Incorrect import style (e.g., destructuring non-existent export).
fix
Use 'const expressWinston = require('winston-express-middleware')' and then 'expressWinston.logger(...)'.
error Error: winston.transports.Console is not a constructor ↓
cause Using winston v2+ API with older transport pattern.
fix
Ensure winston version 1.0.x is installed: 'npm install winston@1.0.x'.
error Warning: options.transports is deprecated, use winstonInstance ↓
cause Mixed API usage; both options provided.
fix
Remove 'transports' if using 'winstonInstance', or vice versa.
Warnings
gotcha Must be placed after router but before custom error handlers for error logging to work. ↓
fix Order middleware as: app.use(router), app.use(errorLogger), then your error handler.
deprecated Winston v1 is deprecated; this middleware is not updated for winston v2+. ↓
fix Use a maintained fork like 'express-winston' (available on npm) or update manually.
gotcha The 'transports' option is ignored if 'winstonInstance' is provided. ↓
fix Use either 'transports' or 'winstonInstance', not both.
gotcha 'expressFormat' overrides 'msg' and 'colorStatus' when true. ↓
fix Set 'expressFormat: false' to use custom 'msg' and 'colorStatus'.
gotcha The 'skip' option expects a function that returns boolean; default is false (never skip). ↓
fix Provide a function like (req, res) => res.statusCode < 400 to skip non-errors.
Install
npm install winston-express-middleware yarn add winston-express-middleware pnpm add winston-express-middleware Imports
- expressWinston wrong
const expressWinston = require('winston-express-middleware').defaultcorrectimport expressWinston from 'winston-express-middleware' - logger wrong
const logger = require('winston-express-middleware').loggercorrectimport { logger } from 'winston-express-middleware' - errorLogger wrong
const errorLogger = require('winston-express-middleware').errorLoggercorrectimport { errorLogger } from 'winston-express-middleware'
Quickstart
const winston = require('winston');
const expressWinston = require('winston-express-middleware');
const express = require('express');
const app = express();
// Request logging
app.use(expressWinston.logger({
transports: [new winston.transports.Console({ json: true, colorize: true })],
meta: true,
msg: 'HTTP {{req.method}} {{req.url}}',
expressFormat: true,
colorStatus: true
}));
app.get('/', (req, res) => res.send('Hello'));
// Error logging (after routes)
app.use(expressWinston.errorLogger({
transports: [new winston.transports.Console({ json: true, colorize: true })]
}));
app.use((err, req, res, next) => {
res.status(500).send('Error');
});
app.listen(3000);