# Middleware System

Prax provides a powerful middleware system for intercepting queries before and after execution. Use middleware for logging, metrics, authentication, caching, retry logic, and more.

## Overview

The middleware system allows you to intercept every query before and after execution. Middleware can modify queries, add context, collect metrics, implement caching, and more.

#### Built-in Middleware

- • `LoggingMiddleware` - Query logging with levels
- • `MetricsMiddleware` - Performance metrics
- • `TimingMiddleware` - Execution time tracking
- • `RetryMiddleware` - Automatic retry with backoff

#### Use Cases

- • Query logging and debugging
- • Performance monitoring
- • Multi-tenant data isolation
- • Query caching
- • Circuit breaking

## Logging Middleware

Log all queries with configurable detail levels. Automatically detect slow queries and log warnings.

#### Log Levels

Level | Description
--- | ---
`Off` | No logging
`Error` | Only errors
`Warn` | Errors and slow queries
`Info` | All queries (default)
`Debug` | Queries with parameters
`Trace` | Everything including responses

## Metrics Middleware

Collect query performance metrics for monitoring and optimization.

#### Available Metrics

`total_queries`

Total query count

`successful_queries`

Successful count

`failed_queries`

Failed count

`avg_time_us`

Average time (μs)

`slow_queries`

Slow query count

`cache_hits`

Cache hit count

## Retry Middleware

Automatically retry failed queries with exponential backoff. Perfect for handling transient errors.

## Custom Middleware

Create your own middleware by implementing the `Middleware` trait.

## Query Context

The `QueryContext` provides information about the current query and allows modification.