> ## Documentation Index
> Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Response Log

> Response logging middleware for debugging and monitoring.

## 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

```go theme={null}
import "github.com/go-mizu/mizu/middlewares/responselog"
```

## Quick Start

```go theme={null}
app := mizu.New()

app.Use(responselog.New())
```

## Configuration

### Options

| Option        | Type           | Default | Description       |
| ------------- | -------------- | ------- | ----------------- |
| `Logger`      | `*slog.Logger` | Default | Logger instance   |
| `IncludeBody` | `bool`         | `false` | Log response body |
| `MaxBodySize` | `int`          | `1024`  | Max body to log   |

## Examples

### Basic Logging

```go theme={null}
app.Use(responselog.New())
```

### Include Response Body

```go theme={null}
app.Use(responselog.WithOptions(responselog.Options{
    IncludeBody: true,
    MaxBodySize: 4096,
}))
```

### With Custom Logger

```go theme={null}
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
app.Use(responselog.WithLogger(logger))
```

## API Reference

### Functions

```go theme={null}
// 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 Case                      | Description                                   | Expected Behavior                                         |
| ------------------------------ | --------------------------------------------- | --------------------------------------------------------- |
| `TestNew`                      | Basic middleware creation with default logger | Logs response with status code 200                        |
| `TestWithOptions_LogBody`      | Enable response body logging                  | Response body content appears in logs                     |
| `TestWithOptions_LogHeaders`   | Enable response header logging                | Response headers appear in logs                           |
| `TestWithOptions_SkipPaths`    | Skip logging for specific paths               | Configured paths (e.g., /health) are not logged           |
| `TestWithOptions_SkipStatuses` | Skip logging for specific status codes        | Configured status codes (e.g., 200) are not logged        |
| `TestWithOptions_MaxBodySize`  | Limit logged body size                        | Response body is truncated to MaxBodySize bytes           |
| `TestErrorsOnly`               | Log only error responses                      | Success responses are skipped, error responses are logged |
| `TestDurationLogged`           | Verify duration tracking                      | Request duration appears in logs                          |
| `TestSizeLogged`               | Verify size tracking                          | Response body size appears in logs                        |

## Related Middlewares

* [requestlog](/middlewares/requestlog) - Request logging
* [bodydump](/middlewares/bodydump) - Body dumping
* [logger](/middlewares/logger) - Simple logging
