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>
impl<State> TracingMiddleware<State>
Sourcepub fn exclude<T: Into<String>>(self, path: T) -> Self
pub fn exclude<T: Into<String>>(self, path: T) -> Self
Ignore and do not log access info for specified path.
Sourcepub fn exclude_regex<T: Into<String>>(self, path: T) -> Self
pub fn exclude_regex<T: Into<String>>(self, path: T) -> Self
Ignore and do not log access info for paths that match regex
Sourcepub fn custom_request_replace(
self,
label: &str,
f: impl Fn(&Request<State>) -> String + Send + Sync + 'static,
) -> Self
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.
Sourcepub fn custom_response_replace(
self,
label: &str,
f: impl Fn(&Response) -> String + Send + Sync + 'static,
) -> Self
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.