Skip to main content

Import

Usage

Options

The logger middleware accepts an optional print function:
(str: string, ...rest: string[]) => void
default:"console.log"
Optional function for customized logging behavior. This function will be called with the formatted log message.

Signature

Examples

Basic usage

Custom print function

Log to file

Structured logging

Conditional logging

Apply to specific routes

Output Format

The logger produces two lines per request:
  1. Incoming request: --> METHOD PATH
  2. Outgoing response: <-- METHOD PATH STATUS DURATION

Examples

Status Code Colors

When color is enabled (supported terminal), status codes are color-coded:
  • 2xx (Success): Green
  • 3xx (Redirect): Cyan
  • 4xx (Client Error): Yellow
  • 5xx (Server Error): Red

Behavior

  • Logs incoming request before handler execution
  • Logs outgoing response after handler completes
  • Includes response status code and elapsed time
  • Time is displayed in milliseconds for durations < 1 second
  • Time is displayed in seconds (rounded) for durations >= 1 second
  • Automatically detects color support in terminal
  • Works with all HTTP methods
  • Extracts path from full URL for cleaner logs