morgan
HTTP request logger middleware for Express — log incoming requests with timing, status codes, and response sizes.
morgan
ExpressHTTP request logger middleware for Express — log incoming requests with timing, status codes, and response sizes.
Fit
Bundle (gzip)
5.3 kB
14.0 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Debugging Express APIs without request logs means flying blind — you can't see which routes are being hit, how long they take, or which requests are returning errors without adding console.log to every handler.
What It Does
Morgan logs HTTP requests in configurable formats. The built-in 'dev' format shows method, URL, status, response time, and size in a color-coded one-liner. The 'combined' format matches Apache's production log format for ingestion by log aggregators. Custom tokens let you log any request/response property.
Installation
npm install morgan
npm install -D @types/morganUsage Example
import express from 'express';
import morgan from 'morgan';
import fs from 'fs';
import path from 'path';
const app = express();
// Colorized development logging
app.use(morgan('dev'));
// Production: write to rotating log file
const accessLog = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' });
app.use(morgan('combined', { stream: accessLog }));
// Custom format with request ID
morgan.token('id', (req: any) => req.id);
app.use(morgan(':id :method :url :status :response-time ms'));Related packages
Node.js body parsing middleware — populate `req.body` from JSON, urlencoded, text, and raw payloads.
compressionExpressHTTP response compression middleware for Express — gzip/deflate responses to reduce bandwidth and improve load times.
cookie-parserExpressCookie parsing middleware for Express — parse Cookie headers into `req.cookies` and support signed cookies for tamper detection.
corsExpressCORS middleware for Express — configure cross-origin resource sharing with a single line of code.
express-async-errorsExpressSimplify Express error handling by automatically passing thrown errors to `next()`.