logo
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

VariableDescription
%%The percent sign
%aPeer IP address (or IP address of reverse proxy if used)
%tTime when the request started processing (in RFC 3339 format)
%rFirst line of request (Example: GET /test HTTP/1.1)
%sResponse status code
%bSize of response in bytes, including HTTP headers
%TTime taken to serve the request, in seconds to 6 decimal places
%DTime taken to serve the request, in milliseconds
%URequest URL
%{r}a“Real IP” remote address *
%{FOO}irequest.headers["FOO"]
%{FOO}oresponse.headers["FOO"]
%{FOO}eenv_var["FOO"]
%{FOO}xiCustom 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

Create Logger middleware with the specified format.

Ignore and do not log access info for specified path.

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

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

Formats the value using the given formatter. Read more

Create Logger middleware with format:

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

Responses produced by the service.

Errors produced by the service.

The TransformService value created by this factory

Errors produced while building a transform service.

The future response value.

Creates and returns a new Transform component, asynchronously

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

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

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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