Socket
Socket
Sign inDemoInstall

express-winston

Package Overview
Dependencies
Maintainers
3
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-winston

express.js middleware for flatiron/winston


Version published
Weekly downloads
392K
increased by1.03%
Maintainers
3
Weekly downloads
 
Created

What is express-winston?

express-winston is a middleware for logging HTTP requests and responses in Express applications. It integrates with the winston logging library, allowing you to log requests and responses in a structured and customizable way.

What are express-winston's main functionalities?

Request Logging

This feature allows you to log incoming HTTP requests. The code sample demonstrates how to set up express-winston to log requests to the console with a specific format.

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

const app = express();

app.use(expressWinston.logger({
  transports: [
    new winston.transports.Console()
  ],
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.json()
  ),
  meta: true, // optional: control whether you want to log the meta data about the request (default to true)
  msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message.
  expressFormat: true, // Use the default Express/morgan request formatting
  colorize: false, // Color the text and status code, using the Express/morgan color palette
  ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
}));

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

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Error Logging

This feature allows you to log errors that occur during the handling of requests. The code sample demonstrates how to set up express-winston to log errors to the console.

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

const app = express();

app.use(expressWinston.errorLogger({
  transports: [
    new winston.transports.Console()
  ],
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.json()
  )
}));

app.get('/', (req, res) => {
  throw new Error('Something went wrong!');
});

app.use((err, req, res, next) => {
  res.status(500).send('Internal Server Error');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Custom Transports

This feature allows you to use custom transports for logging. The code sample demonstrates how to set up express-winston to log requests to a file.

const express = require('express');
const winston = require('winston');
const expressWinston = require('express-winston');
const fs = require('fs');

const app = express();

const fileTransport = new winston.transports.File({ filename: 'logfile.log' });

app.use(expressWinston.logger({
  transports: [
    fileTransport
  ],
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  )
}));

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

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to express-winston

Keywords

FAQs

Package last updated on 16 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc