[−][src]Struct tracing_actix_web::TracingLogger
TracingLogger
is a middleware to log request and response info in a structured format.
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.
Usage
Register TracingLogger
as a middleware for your application using .wrap
on App
.
Add a Subscriber
implementation to output logs to the console.
use actix_web::middleware::Logger; use actix_web::App; use tracing::{Subscriber, subscriber::set_global_default}; use tracing_actix_web::TracingLogger; use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; /// Compose multiple layers into a `tracing`'s subscriber. pub fn get_subscriber( name: String, env_filter: String ) -> impl Subscriber + Send + Sync { let env_filter = EnvFilter::try_from_default_env() .unwrap_or(EnvFilter::new(env_filter)); let formatting_layer = BunyanFormattingLayer::new( name.into(), std::io::stdout ); Registry::default() .with(env_filter) .with(JsonStorageLayer) .with(formatting_layer) } /// Register a subscriber as global default to process span data. /// /// It should only be called once! pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) { LogTracer::init().expect("Failed to set logger"); set_global_default(subscriber).expect("Failed to set subscriber"); } fn main() { let subscriber = get_subscriber("app".into(), "info".into()); init_subscriber(subscriber); let app = App::new().wrap(TracingLogger); }
Trait Implementations
impl<S, B> Transform<S> for TracingLogger where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
[src]
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
type Request = ServiceRequest
Requests handled by the service.
type Response = ServiceResponse<B>
Responses given by the service.
type Error = Error
Errors produced by the service.
type Transform = TracingLoggerMiddleware<S>
The TransformService
value created by this factory
type InitError = ()
Errors produced while building a transform service.
type Future = Ready<Result<Self::Transform, Self::InitError>>
The future response value.
fn new_transform(&self, service: S) -> Self::Future
[src]
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E> where
F: Fn(Self::InitError) -> E + Clone,
F: Fn(Self::InitError) -> E + Clone,
Auto Trait Implementations
impl RefUnwindSafe for TracingLogger
impl Send for TracingLogger
impl Sync for TracingLogger
impl Unpin for TracingLogger
impl UnwindSafe for TracingLogger
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
fn instrument(self, span: Span) -> Instrumented<Self>
[src]
fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,
fn vzip(self) -> V
impl<T> WithSubscriber for T
[src]
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
[src]
S: Into<Dispatch>,