actix-contrib-logger
Logger middleware for the Actix Web framework.
Actually it's a copy & paste from the official Logger middleware (original source code), but it allows to choose the logging level depending on the HTTP status code responded, (see Logger::custom_level()
and Logger::custom_error_resp_level()
) and by default server errors are logged with ERROR
level.
Moreover, error in response logs are also configurable, and by default logged as ERROR
for server side failures.
The Logger middleware uses the standard log crate to log information. You should enable logger for the http_logger
to see access logs (env_logger
or similar).
Examples
In the following example ERROR
level is used for server errors, WARN
for HTTP 404 responses (Not Found), and for the rest INFO
level:
use actix_contrib_logger::middleware::Logger;
use env_logger::Env;
use http::StatusCode;
use log::Level;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
env_logger::init_from_env(Env::default().default_filter_or("info"));
HttpServer::new(|| {
let logger = Logger::default()
.custom_level(|status| {
if status.is_server_error() {
Level::Error
} else if status == StatusCode::NOT_FOUND {
Level::Warn
} else {
Level::Info
}
});
App::new().wrap(logger)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Requests logs will look like:
[2023-08-13T07:28:00Z INFO http_logger] 127.0.0.1 "GET / HTTP/1.1" 200 802 "-" "Mozilla/5.0 ..." 0.001985
[2023-08-13T07:29:10Z ERROR http_logger] 127.0.0.1 "POST /users HTTP/1.1" 500 86 "-" "curl/7.68.0" 0.002023
[2023-08-13T07:29:10Z WARN http_logger] 127.0.0.1 "PUT /users HTTP/1.1" 404 55 "-" "HTTPie/3.2.1" 0.002023
Using the method logger.custom_error_resp_level()
the log level of error in responses are also configurable, otherwise by default like the original logger all are printed in DEBUG
level, except for server errors that are printed out in ERROR
level. The log also includes the HTTP status information (original logger doesn't). E.g.:
[2023-08-13T20:59:53Z ERROR http_logger] Error in "500 Internal Server Error" response: DB(PoolTimedOut)
About
Project Home: https://github.com/mrsarm/rust-actix-contrib-logger.
Authors
- Original Authors: The Actix Project created the original
logger
module. - Modifications in this project: Mariano Ruiz (mrsarm at gmail.com).
License
This project is licensed under either of the following licenses, at your option:
- Apache License, Version 2.0, (LICENSE-APACHE or [http://www.apache.org/licenses/LICENSE-2.0])
- MIT license (LICENSE-MIT or [http://opensource.org/licenses/MIT])