Struct actix_web::middleware::Logger
source · pub struct Logger(/* private fields */);
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” |
%{FOO}xo | Custom response 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§
source§impl 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 log_target(self, target: impl Into<Cow<'static, str>>) -> Self
pub fn log_target(self, target: impl Into<Cow<'static, str>>) -> Self
Sets the logging target to target
.
By default, the log target is module_path!()
of the log call location. In our case, that
would be actix_web::middleware::logger
.
Examples
Using .log_target("http_log")
would have this effect on request logs:
- [2015-10-21T07:28:00Z INFO actix_web::middleware::logger] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985
+ [2015-10-21T07:28:00Z INFO http_log] 127.0.0.1 "GET / HTTP/1.1" 200 88 "-" "dmc/1.0" 0.001985
^^^^^^^^
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.
Examples
Logger::new("example %{JWT_ID}xi")
.custom_request_replace("JWT_ID", |req| parse_jwt_id(req.headers().get("Authorization")));
sourcepub fn custom_response_replace(
self,
label: &str,
f: impl Fn(&ServiceResponse) -> String + 'static
) -> Self
pub fn custom_response_replace( self, label: &str, f: impl Fn(&ServiceResponse) -> String + 'static ) -> Self
Register a function that receives a ServiceResponse
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.
The replacement function does not have access to the response body.
Examples
fn log_if_error(res: &ServiceResponse) -> String {
if res.status().as_u16() >= 400 {
"ERROR".to_string()
} else {
"-".to_string()
}
}
Logger::new("example %{ERROR_STATUS}xo")
.custom_response_replace("ERROR_STATUS", |res| log_if_error(res) );