Skip to main content

Overview

The responselog middleware logs HTTP response details including status codes, response times, and response sizes. Use it when you need:
  • Response monitoring
  • Debugging response issues
  • Performance tracking

Installation

import "github.com/go-mizu/mizu/middlewares/responselog"

Quick Start

app := mizu.New()

app.Use(responselog.New())

Configuration

Options

OptionTypeDefaultDescription
Logger*slog.LoggerDefaultLogger instance
IncludeBodyboolfalseLog response body
MaxBodySizeint1024Max body to log

Examples

Basic Logging

app.Use(responselog.New())

Include Response Body

app.Use(responselog.WithOptions(responselog.Options{
    IncludeBody: true,
    MaxBodySize: 4096,
}))

With Custom Logger

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
app.Use(responselog.WithLogger(logger))

API Reference

Functions

// New creates response log middleware
func New() mizu.Middleware

// WithLogger creates with custom logger
func WithLogger(logger *slog.Logger) mizu.Middleware

// WithOptions creates with configuration
func WithOptions(opts Options) mizu.Middleware

Technical Details

The responselog middleware captures response data through a custom ResponseWriter wrapper:

Response Capture Mechanism

The middleware uses a responseCapture struct that wraps the standard http.ResponseWriter:
  • Intercepts WriteHeader() to capture the status code
  • Intercepts Write() to capture response body up to MaxBodySize
  • Automatically restores the original writer after processing

Logging Behavior

Response logs include:
  • Method: HTTP request method
  • Path: Request URL path
  • Status: HTTP status code
  • Duration: Time taken to process the request
  • Size: Response body size in bytes
  • Headers: Response headers (if LogHeaders is enabled)
  • Body: Response body content (if LogBody is enabled, truncated to MaxBodySize)

Log Levels

The middleware automatically sets log levels based on status codes:
  • Status >= 400: Logged as ERROR
  • Status < 400: Logged as INFO

Filtering

Two filtering mechanisms are available:
  • SkipPaths: Paths that should not be logged at all
  • SkipStatuses: Status codes that should not be logged
Paths are checked before request processing, while statuses are checked after.

Default Values

  • MaxBodySize: 4KB (4096 bytes)
  • LogBody: false
  • LogHeaders: false
  • Logger: Default slog text handler to stdout

Best Practices

  • Be careful with body logging (sensitive data)
  • Use for debugging only
  • Set reasonable body size limits
  • Combine with request logging

Testing

The middleware includes comprehensive test coverage for all features:
Test CaseDescriptionExpected Behavior
TestNewBasic middleware creation with default loggerLogs response with status code 200
TestWithOptions_LogBodyEnable response body loggingResponse body content appears in logs
TestWithOptions_LogHeadersEnable response header loggingResponse headers appear in logs
TestWithOptions_SkipPathsSkip logging for specific pathsConfigured paths (e.g., /health) are not logged
TestWithOptions_SkipStatusesSkip logging for specific status codesConfigured status codes (e.g., 200) are not logged
TestWithOptions_MaxBodySizeLimit logged body sizeResponse body is truncated to MaxBodySize bytes
TestErrorsOnlyLog only error responsesSuccess responses are skipped, error responses are logged
TestDurationLoggedVerify duration trackingRequest duration appears in logs
TestSizeLoggedVerify size trackingResponse body size appears in logs