tracing-actix-web

Structured logging for actix-web applications.


tracing-actix-web provides [TracingLogger], a middleware to log request and response info when using the [actix-web] framework.

[TracingLogger] is designed as a drop-in replacement of [actix-web]'s [Logger].

[Logger] is built on top of the [log] crate: you need to use regular expressions to parse the request information out of the logged message.

[TracingLogger] relies on [tracing], a modern instrumentation framework for structured logging: all request information is captured as a machine-parsable set of key-value pairs.
It also enables propagation of context information to children spans.

How to install

Add tracing-actix-web to your dependencies: ```toml [dependencies]

...

tracing-actix-web = "0.2" If you are using [`cargo-edit`](https://github.com/killercup/cargo-edit), run bash cargo add tracing-actix-web ```

tracing-actix-web 0.2.x depends on actix-web 3.x.x.
If you are using actix-web 2.x.x use tracing-actix-web 0.1.x.

Usage example

Register TracingLogger as a middleware for your application using .wrap on App.
Add a Subscriber implementation to output logs to the console.

```rust use actixweb::middleware::Logger; use actixweb::App; use tracing::{Subscriber, subscriber::setglobaldefault}; use tracingactixweb::TracingLogger; use tracingbunyanformatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};

/// Compose multiple layers into a tracing's subscriber. pub fn getsubscriber( name: String, envfilter: String ) -> impl Subscriber + Send + Sync { let envfilter = EnvFilter::tryfromdefaultenv() .unwrapor(EnvFilter::new(envfilter)); let formattinglayer = BunyanFormattingLayer::new( name.into(), std::io::stdout ); Registry::default() .with(envfilter) .with(JsonStorageLayer) .with(formatting_layer) }

/// Register a subscriber as global default to process span data. /// /// It should only be called once! pub fn initsubscriber(subscriber: impl Subscriber + Send + Sync) { LogTracer::init().expect("Failed to set logger"); setglobal_default(subscriber).expect("Failed to set subscriber"); }

fn main() { let subscriber = getsubscriber("app".into(), "info".into()); initsubscriber(subscriber);

let app = App::new().wrap(TracingLogger);

} ```