# Morgan — HTTP Request Logger Middleware for Node.js > Morgan is an HTTP request logger middleware for Node.js that outputs structured access logs in predefined or custom formats, used widely with Express for development debugging and production logging. ## Install Save in your project root: # Morgan — HTTP Request Logger Middleware for Node.js ## Quick Use ```javascript const express = require('express'); const morgan = require('morgan'); const app = express(); app.use(morgan('combined')); app.get('/', (req, res) => res.send('Hello')); app.listen(3000); // Logs: ::1 - - [09/May/2026:12:00:00 +0000] "GET / HTTP/1.1" 200 5 ``` ## Introduction Morgan is the standard HTTP request logging middleware for Express applications. It intercepts every request, records timing and metadata, and writes formatted log lines to a configurable output stream. It ships with predefined formats like Apache combined and common, and supports custom token-based formats. ## What Morgan Does - Logs HTTP requests with method, URL, status code, response time, and content length - Provides predefined formats: combined, common, dev, short, and tiny - Supports custom format strings using tokens like `:method`, `:url`, `:status` - Writes to stdout by default or to any writable stream including files - Allows conditional logging by skipping requests based on status code or other criteria ## Architecture Overview Morgan registers itself as Express middleware that runs on every request. It captures the start time, hooks into the response `finish` event, and calculates response duration. When the response completes, it formats the log entry by replacing tokens in the format string with actual values from the request and response objects. The formatted string is written to the configured output stream. Custom tokens can be registered to log application-specific data like user IDs or correlation IDs. ## Self-Hosting & Configuration - Install via npm: `npm install morgan` - Use predefined formats: `morgan('dev')` for color-coded development logs - Create custom formats: `morgan(':method :url :status :response-time ms')` - Log to a file: `morgan('combined', { stream: fs.createWriteStream('access.log', { flags: 'a' }) })` - Skip logging for successful requests: `morgan('combined', { skip: (req, res) => res.statusCode < 400 })` ## Key Features - Predefined formats match standard Apache log formats for compatibility with log analyzers - Custom tokens let you add application-specific fields to log lines - Response time measurement is built in with millisecond precision - Stream-based output works with file rotation tools like `rotating-file-stream` - Conditional skip function enables logging only errors or specific routes ## Comparison with Similar Tools - **Winston** — general-purpose logger with transports and log levels; Morgan is HTTP-request-specific - **Pino** — high-performance JSON logger; Morgan outputs text-based access logs - **Bunyan** — structured JSON logging; Morgan focuses on request/response logging only - **express-winston** — combines Winston with Express request logging; Morgan is simpler and more focused ## FAQ **Q: Can I use Morgan in production?** A: Yes. Use the `combined` format for Apache-compatible production logs and pipe output to a file or log aggregator. **Q: How do I rotate log files?** A: Use the `rotating-file-stream` package as the output stream to create daily or size-based log rotation. **Q: Does Morgan log request bodies?** A: No. Morgan logs request metadata (method, URL, status, timing). Use a separate middleware for body logging. **Q: Can I use Morgan with Koa or Fastify?** A: Morgan is designed for Express. For Koa, use `koa-morgan` or `koa-logger`. For Fastify, use its built-in logger. ## Sources - https://github.com/expressjs/morgan - https://www.npmjs.com/package/morgan --- Source: https://tokrepo.com/en/workflows/asset-0b407cd8 Author: AI Open Source