TracingMiddleware

Struct TracingMiddleware 

Source
pub struct TracingMiddleware<State: Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

TracingMiddleware for logging request and response info to the terminal.

§Usage

Create TracingMiddleware middleware with the specified format. Default TracingMiddleware could be created with default method, it uses the default format:

%a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T
use tide::{Request, Response, StatusCode};
use tide_tracing_middleware::TracingMiddleware;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[async_std::main]
async fn main() -> tide::Result<()> {
    FmtSubscriber::builder().with_max_level(Level::DEBUG).init();

    let mut app = tide::new();
    app.with(TracingMiddleware::default());
    app.at("/index").get(index);
    app.listen("127.0.0.1:8080").await?;
    Ok(())
}

async fn index(_req: Request<()>) -> tide::Result {
    let res = Response::builder(StatusCode::Ok)
        .body("hello world!")
        .build();
    Ok(res)
}

§Format

  • %%: The percent sign
  • %a: Remote IP-address (IP-address of proxy if using reverse proxy)
  • %t: Time when the request was started to process (in rfc3339 format)
  • %r: First line of request
  • %s: Response status code
  • %b: Size of response body in bytes, not including HTTP headers
  • %T: Time taken to serve the request, in seconds with floating fraction in .06f format
  • %D: Time taken to serve the request, in milliseconds
  • %U: Request URL
  • %M: Request method
  • %V: Request HTTP version
  • %Q: Request URL’s query string
  • %{r}a: Real IP remote address *
  • %{FOO}i: request.headers[‘FOO’]
  • %{FOO}o: response.headers[‘FOO’]
  • %{FOO}e: os.environ[‘FOO’]
  • %{FOO}xi: custom request replacement labelled “FOO”
  • %{FOO}xo: custom response replacement labelled “FOO”

Implementations§

Source§

impl<State> TracingMiddleware<State>
where State: Clone + Send + Sync + 'static,

Source

pub fn new(s: &str) -> Self

Create TracingMiddleware middleware with the specified format.

Source

pub fn exclude<T: Into<String>>(self, path: T) -> Self

Ignore and do not log access info for specified path.

Source

pub fn exclude_regex<T: Into<String>>(self, path: T) -> Self

Ignore and do not log access info for paths that match regex

Source

pub fn custom_request_replace( self, label: &str, f: impl Fn(&Request<State>) -> String + Send + Sync + 'static, ) -> Self

Register a function that receives a Request and returns a String for use in the log line. The label passed as the first argument should match a replacement substring in the logger format like %{label}xi.

It is convention to print “-” to indicate no output instead of an empty string.

Source

pub fn custom_response_replace( self, label: &str, f: impl Fn(&Response) -> String + Send + Sync + 'static, ) -> Self

Register a function that receives a Response and returns a String for use in the log line. The label passed as the first argument should match a replacement substring in the logger format like %{label}xo.

It is convention to print “-” to indicate no output instead of an empty string.

Source

pub fn gen_tracing_span(self, f: fn(&Request<State>) -> Span) -> Self

Trait Implementations§

Source§

impl<State: Clone + Send + Sync + 'static> Default for TracingMiddleware<State>

Source§

fn default() -> Self

Create TracingMiddleware middleware with format:

%a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T
Source§

impl<State> Middleware<State> for TracingMiddleware<State>
where State: Clone + Send + Sync + 'static,

Source§

fn handle<'life0, 'life1, 'async_trait>( &'life0 self, request: Request<State>, next: Next<'life1, State>, ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Asynchronously handle the request, and return a response.
Source§

fn name(&self) -> &str

Set the middleware’s name. By default it uses the type signature.

Auto Trait Implementations§

§

impl<State> Freeze for TracingMiddleware<State>

§

impl<State> !RefUnwindSafe for TracingMiddleware<State>

§

impl<State> Send for TracingMiddleware<State>

§

impl<State> Sync for TracingMiddleware<State>

§

impl<State> Unpin for TracingMiddleware<State>

§

impl<State> !UnwindSafe for TracingMiddleware<State>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,