API Reference

morgan

HTTP request logger middleware for Node and Express with predefined and custom log formats.

Official Documentation ↗
CompatibilityThis documentation covers version morgan v1.10.x / Express 4.x / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install morgan
pnpmpnpm add morgan
yarnyarn add morgan

Methods

Overview

morgan records HTTP requests as they complete, with predefined formats for common needs and a token system for custom fields. It integrates directly with Express middleware ordering.

Core Methods

Apply a format with morgan, define tokens and formats for custom fields, and use the skip option to exclude noisy routes. A stream option redirects output to a file or logger.

Usage Notes

Log requests after the response finishes so status and duration are available. Exclude health checks, and add a request id token so logs can be correlated across services.

Common Pitfalls

A verbose format logs sensitive query strings and headers that may contain tokens, so redact them. Logging every static asset inflates log volume, so filter or split routes by concern.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/expressjs/morgan@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:59.621Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Which format should I use?
Use combined for rich production logs compatible with common analyzers, common for Apache-style logs, and a custom format if you emit structured JSON. Token names are documented with the package.
How do I log response time?
The response-time token reports milliseconds. Add it to a custom format. It excludes time spent writing the body to the client, so treat it as handler time.
Can morgan log JSON?
morgan outputs text formats. For structured logging, define a custom format that serializes fields or use a dedicated logger and log within a request-scoped middleware.