Struct actix_web::middleware::Logger
source · [−]pub struct Logger(_);
Expand description
Middleware for logging request and response summaries to the terminal.
This middleware uses the log
crate to output information. Enable log
’s output for the
“actix_web” scope using env_logger
or similar crate.
Default Format
The default
Logger uses the following format:
%a "%r" %s %b "%{Referer}i" "%{User-Agent}i" %T
Example Output:
127.0.0.1:54278 "GET /test HTTP/1.1" 404 20 "-" "HTTPie/2.2.0" 0.001074
Examples
use actix_web::{middleware::Logger, App};
// access logs are printed with the INFO level so ensure it is enabled by default
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let app = App::new()
// .wrap(Logger::default())
.wrap(Logger::new("%a %{User-Agent}i"));
Format
Variable | Description |
---|---|
%% | The percent sign |
%a | Peer IP address (or IP address of reverse proxy if used) |
%t | Time when the request started processing (in RFC 3339 format) |
%r | First line of request (Example: GET /test HTTP/1.1 ) |
%s | Response status code |
%b | Size of response in bytes, including HTTP headers |
%T | Time taken to serve the request, in seconds to 6 decimal places |
%D | Time taken to serve the request, in milliseconds |
%U | Request URL |
%{r}a | “Real IP” remote address * |
%{FOO}i | request.headers["FOO"] |
%{FOO}o | response.headers["FOO"] |
%{FOO}e | env_var["FOO"] |
%{FOO}xi | Custom request replacement labelled “FOO” |
Security
* “Real IP” remote address is calculated using
ConnectionInfo::realip_remote_addr()
If you use this value, ensure that all requests come from trusted hosts. Otherwise, it is trivial for the remote client to falsify their source IP address.
Implementations
sourceimpl Logger
impl Logger
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(&ServiceRequest) -> String + 'static
) -> Self
pub fn custom_request_replace(
self,
label: &str,
f: impl Fn(&ServiceRequest) -> String + 'static
) -> Self
Register a function that receives a ServiceRequest 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.
Example
Logger::new("example %{JWT_ID}xi")
.custom_request_replace("JWT_ID", |req| parse_jwt_id(req.headers().get("Authorization")));
Trait Implementations
sourceimpl<S, B> Transform<S, ServiceRequest> for Logger where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
impl<S, B> Transform<S, ServiceRequest> for Logger where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody,
type Response = ServiceResponse<StreamLog<B>>
type Response = ServiceResponse<StreamLog<B>>
Responses produced by the service.
type Transform = LoggerMiddleware<S>
type Transform = LoggerMiddleware<S>
The TransformService
value created by this factory
type Future = Ready<Result<<Logger as Transform<S, ServiceRequest>>::Transform, <Logger as Transform<S, ServiceRequest>>::InitError>>
type Future = Ready<Result<<Logger as Transform<S, ServiceRequest>>::Transform, <Logger as Transform<S, ServiceRequest>>::InitError>>
The future response value.
sourcefn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
Auto Trait Implementations
impl !RefUnwindSafe for Logger
impl !Send for Logger
impl !Sync for Logger
impl Unpin for Logger
impl !UnwindSafe for Logger
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more