Crate outlet_postgres

Crate outlet_postgres 

Source
Expand description

§outlet-postgres

PostgreSQL logging handler for the outlet HTTP request/response middleware. This crate implements the RequestHandler trait from outlet to log HTTP requests and responses to PostgreSQL with JSONB serialization for bodies.

§Quick Start

Basic usage:

use outlet::{RequestLoggerLayer, RequestLoggerConfig};
use outlet_postgres::PostgresHandler;
use axum::{routing::get, Router};
use tower::ServiceBuilder;

async fn hello() -> &'static str {
    "Hello, World!"
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let database_url = "postgresql://user:password@localhost/dbname";
    let handler: PostgresHandler = PostgresHandler::new(database_url).await?;
    let layer = RequestLoggerLayer::new(RequestLoggerConfig::default(), handler);

    let app = Router::new()
        .route("/hello", get(hello))
        .layer(ServiceBuilder::new().layer(layer));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
    axum::serve(listener, app).await?;
    Ok(())
}

§Features

  • PostgreSQL Integration: Uses sqlx for async PostgreSQL operations
  • JSONB Bodies: Serializes request/response bodies to JSONB fields
  • Type-safe Querying: Query logged data with typed request/response bodies
  • Correlation: Links requests and responses via correlation IDs
  • Error Handling: Graceful error handling with logging
  • Flexible Serialization: Generic error handling for custom serializer types

Re-exports§

pub use error::PostgresHandlerError;
pub use repository::HttpRequest;
pub use repository::HttpResponse;
pub use repository::RequestFilter;
pub use repository::RequestRepository;
pub use repository::RequestResponsePair;

Modules§

error
Error types for outlet-postgres.
repository

Structs§

PathFilter
Filter configuration for determining which requests to log.
PostgresHandler
PostgreSQL handler for outlet middleware.
SerializationError
Error type for serialization failures with fallback data.

Functions§

migrator
Get the migrator for running outlet-postgres database migrations.