1 unstable release

0.1.0 Aug 14, 2023

#571 in HTTP server

Download history 1/week @ 2023-12-11 27/week @ 2023-12-18 47/week @ 2023-12-25 13/week @ 2024-01-01 44/week @ 2024-01-08 28/week @ 2024-01-15 11/week @ 2024-02-19 17/week @ 2024-02-26 10/week @ 2024-03-04 19/week @ 2024-03-11 44/week @ 2024-03-18 122/week @ 2024-03-25

197 downloads per month

MIT/Apache

46KB
825 lines

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 in server side failures.

The Logger middleware uses the standard log crate to log information. You should enable logger for http_logger to see access log (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:

Dependencies

~18–32MB
~550K SLoC