Drop-in replacement for the Actix Web HTTP Logger middleware

Overview

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:

You might also like...
zoxide is a blazing fast replacement for your cd command
zoxide is a blazing fast replacement for your cd command

zoxide A smarter cd command for your terminal zoxide is a blazing fast replacement for your cd command, inspired by z and z.lua. It keeps track of the

procs is a replacement for ps written in Rust.
procs is a replacement for ps written in Rust.

procs is a replacement for ps written in Rust. Documentation quick links Features Platform Installation Usage Configuration Features Output by t

fastmod is a fast partial replacement for the codemod tool

fastmod is a fast partial replacement for codemod. Like codemod, it is a tool to assist you with large-scale codebase refactors, and it supports most of codemod's options.

A readline replacement written in Rust

A readline replacement written in Rust Basic example // Create a default reedline object to handle user input use reedline::{DefaultPrompt, Reedline,

Fls - Ferris-LS, a very bad LS replacement. Besides that, proves that I suck at clean & good code.

FLS A handy ls remake, purely for learning. Why FLS? There's no reason, at all, don't use it. I just want to learn Rust :D Usage Flags: -i = Use icons

πŸ¦€οΈatos for linux by rust - A partial replacement for Apple's atos tool for converting addresses within a binary file to symbols.

atosl-rs πŸ¦€οΈ atos for linux by rust - A partial replacement for Apple's atos tool for converting addresses within a binary file to symbols. tested on

xcp is a (partial) clone of the Unix cp command. It is not intended as a full replacement

xcp is a (partial) clone of the Unix cp command. It is not intended as a full replacement, but as a companion utility with some more user-friendly feedback and some optimisations that make sense under certain tasks (see below).

Xcode Neovim Replacement-ish.
Xcode Neovim Replacement-ish.

An XCode replacement-ish development environment that aims to be your reliable XCode alternative to develop exciting new [apple] software products πŸš€

A more convenient `#[target_feature]` replacement

A more convenient #[target_feature] replacement To get good performance out of SIMD everything on the SIMD codepath must be inlined. With how SIMD is

Releases(0.1.0)
Owner
Mariano Ruiz
Software Developer and Consultant with 14+ years of hands-on experience.
Mariano Ruiz
drop-in replacement for libfuzzer

Fazi A reimplementation of libfuzzer in Rust with some improvements Supported Features libFuzzer's mutations SanCov feedback Building without a main()

Lander Brandt 56 Nov 2, 2022
A lightweight, no-fuss, drop-in replacement for Rudderstack

Welcome to Stilgar! Stilgar is a lightweight, no-fuss, drop-in replacement for Rudderstack. Key features: Seamlessly compatible with all Rudderstack c

Withings 4 Jul 21, 2023
A drop-in replacement for std::time::Instant that is faster and more accurate.

Fastant A drop-in replacement for std::time::Instant that measures time with high performance and high accuracy powered by Time Stamp Counter (TSC). U

FastLabs 8 Sep 15, 2024
πŸ”₯ πŸ“ (fwdt) "few word do trick" is a cross platform manual fast logger

Few Word Do Trick (fwdt) Few Word Do Trick (fwdt) is a cross-platform general purpose fast logger for humans that supports incomplete csvs for a bette

Alex Hallam 15 Feb 3, 2023
βš™οΈ Pre-commit hook for downgrading Python logger syntax

printf-log-formatter Automatically convert f-strings and str.format() syntax to printf-style strings. In other words, this syntax logger.error(f"{1}")

snok 5 Apr 25, 2023
Middleware/ios shortcut to setup alarms automatically based on the first class

Webuntis alarm This is a small little "middleware" / web server intended to connect to a webuntis timetable used in german schools which i wrote when

raizo 3 Dec 30, 2022
A tiling layout engine for egui with drag-and-drop and resizing

egui_tiles Layouting and docking for egui. Supports: Horizontal and vertical layouts Grid layouts Tabs Drag-and-drop docking Trying it cargo r --examp

rerun.io 83 Jun 5, 2023
General Rust Actix Applications and AWS Programming Utilities

RUST Actix-Web Microservice Our Rust Beginners Kit for Application Development A collection of sample code using the actix rust framework to A) Develo

IntelliConnect Technologies 58 Nov 21, 2022
πŸ¦€πŸ”¨ DevBcn Workshop - Full Stack Rust - Actix - Postgres - Shuttle - Dioxus

Building a Movie Collection Manager - Full Stack Workshop with Rust, Actix, SQLx, Dioxus, and Shuttle Welcome to the this workshop! In this hands-on w

BcnRust 57 Jul 12, 2023
exa is a modern replacement for ls.

exa exa is a modern replacement for ls. README Sections: Options β€” Installation β€” Development exa is a modern replacement for the venerable file-listi

Benjamin Sago 20.3k Jan 8, 2023