Module access_log_layer

Source
Expand description

AccessLogLayer: A standalone Tower Layer producing Apache/NCSA Common Log Format (CLF) lines with log level determined by HTTP status:

  • error: 5xx
  • warn : 4xx
  • debug: 2xx / 3xx

Also emits a separate warn log when Time To First Byte (TTFB) exceeds the configured threshold (default 150ms) for successful (2xx/3xx) responses.

Does not currently support RFC 1413 identities, nor logging the user (whether via basic access authentication, digest access authentication, or otherwise).

Integrate by adding (and enabling ConnectInfo if you want remote IP):

โ“˜
  use crate::io::webui::access_log_layer::AccessLogLayer;
  app.layer(AccessLogLayer::new(Duration::from_millis(150)))

Ensure you serve with into_make_service_with_connect_info::<SocketAddr>() to populate ConnectInfo<SocketAddr> for remote address logging:

โ“˜
  axum::serve(listener, app.into_make_service_with_connect_info::<SocketAddr>())
      .await?;

Dependencies you need in Cargo.toml (if not already present):

  chrono = { version = "0.4", features = ["clock"] }
  pin-project = "1"

This module does NOT depend on tower_http::trace::TraceLayer.

Explanation of some of this: https://raw.githubusercontent.com/tower-rs/tower/refs/heads/master/guides/building-a-middleware-from-scratch.md

Modulesยง

prelude

Structsยง

AccessLogFuture
AccessLogLayer
AccessLogService
AccessLogState ๐Ÿ”’
CountingBody
SharedLog ๐Ÿ”’

Functionsยง

http_version_str ๐Ÿ”’