express-winston-2

raw JSON →
0.0.2 verified Sat Apr 25 auth: no javascript

Logging middleware for Express.js using winston. Version 0.0.2 (latest). Provides request and error logging with whitelist support for request/response properties, custom log levels based on status code, and skip/ignore functionality. Differentiator: uses winston's flexible transport system and allows fine-grained control over log level via a callback function. Basic functionality, rarely updated.

error Cannot find module 'express-winston-2'
cause Package not installed or typo in module name.
fix
Run 'npm install express-winston-2' and check spelling in require().
error TypeError: expressWinston.logger is not a function
cause Incorrect import or module load failure.
fix
Ensure required correctly: const expressWinston = require('express-winston-2');
error Error: Invalid winston transport
cause Transport object is not an instance of winston.Transport.
fix
Use 'new winston.transports.Console()' or appropriate transport constructor.
error TypeError: Cannot read property 'statusCode' of undefined
cause getLogLevel function expects statusCode argument but response object is passed.
fix
Change getLogLine to getLogLevel and accept (statusCode) instead of (res).
gotcha Package is not ESM compatible; using import syntax will fail.
fix Use require() instead of import.
gotcha The logger middleware must be placed after the router and before custom error handlers for proper error logging.
fix Order middleware as: router, expressWinston.logger, expressWinston.errorLogger, error handlers.
deprecated The whitelist feature is marked as new in 0.2.x but may be removed in future versions.
fix Check changelog before upgrading; consider alternatives like express-winston.
gotcha Setting winstonInstance ignores the transports option; only one should be used.
fix Use either transports or winstonInstance, not both.
gotcha The getLogLevel function receives the response status code, not the response object.
fix Define getLogLevel with parameter (statusCode) to return log level.
npm install express-winston-2
yarn add express-winston-2
pnpm add express-winston-2

Shows how to set up express-winston-2 middleware with console transport and custom message format.

const winston = require('winston');
const expressWinston = require('express-winston-2');
const express = require('express');
const app = express();

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,
  ignoreRoute: function (req, res) { return false; }
}));

app.use(require('./my-router'));

const port = process.env.PORT || 3000;
app.listen(port, () => console.log('Server started'));