Logger Middleware
HTTP request/response logging middleware that logs request metadata and response details with configurable output formats.Installation
Function
logger()
Creates middleware that logs various request/response information.
Signature:
options- Optional logger configuration
Options
LoggerOptions
format
The format to use for log messages. Supports token-based formatting.
- Type:
string - Default:
'[%date] %method %path %status %contentLength'
%date- Date and time in Apache/nginx format (dd/Mon/yyyy:HH:mm:ss ±zzzz)%dateISO- Date and time in ISO format%duration- Request duration in milliseconds%contentLength- Response Content-Length header%contentType- Response Content-Type header%host- Request URL host%hostname- Request URL hostname%method- Request method%path- Request pathname + search%pathname- Request pathname%port- Request port%query- Request query string (search)%referer- Request Referer header%search- Request search string%status- Response status code%statusText- Response status text%url- Full request URL%userAgent- Request User-Agent header
log
The function to use to log messages.
- Type:
(message: string) => void - Default:
console.log
Basic Usage
Custom Format
Apache Combined Log Format
Custom Logger Function
Minimal Format
Detailed Format with Duration
Structured Logging
Conditional Logging
Related
- fetch-router - Router for the web Fetch API