| Crates.io | actix-web-middleware-slogger |
| lib.rs | actix-web-middleware-slogger |
| version | 0.1.0 |
| created_at | 2025-04-07 00:42:44.979231+00 |
| updated_at | 2025-04-07 00:42:44.979231+00 |
| description | Struct based logger middleware for actix-web |
| homepage | https://github.com/tyzhnenko/actix-web-middleware-slogger |
| repository | https://github.com/tyzhnenko/actix-web-middleware-slogger |
| max_upload_size | |
| id | 1623524 |
| size | 85,650 |
A structured logging middleware for Actix Web, inspired by the Logger middleware from actix-web.
By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:
log crate integrationtracing-request-id featureAdd to your Cargo.toml:
[dependencies]
actix-web-middleware-slogger = "0.1.0"
use tokio;
use actix_web;
use actix_web::{web, App, HttpServer};
use actix_web_middleware_slogger::SLogger;
use structured_logger::{Builder, async_json::new_writer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Initialize your logger of choice
Builder::new()
.with_target_writer("*", new_writer(tokio::io::stdout()))
.init();
HttpServer::new(|| {
App::new()
.wrap(SLogger::default())
.route("/", web::get().to(|| async { "Hello world!" }))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Use the tracing-request-id feature to integrate with the tracing ecosystem:
[dependencies]
actix-web-middleware-slogger = { version = "0.1.0", features = ["tracing-request-id"] }
use tokio;
use actix_web;
use actix_web::{web, App, HttpServer};
use actix_web_middleware_slogger::SLogger;
use structured_logger::{Builder, async_json::new_writer};
use tracing_actix_web::TracingLogger;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Initialize your logger of choice
Builder::new()
.with_target_writer("*", new_writer(tokio::io::stdout()))
.init();
HttpServer::new(|| {
App::new()
.wrap(TracingLogger::default())
.wrap(SLogger::default())
.route("/", web::get().to(|| async { "Hello world!" }))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
You can customize which fields are included in your logs:
use actix_web_middleware_slogger::{SLogger, FieldsBuilder};
let logger = SLogger::new(
Fields::builder()
.with_method() // HTTP method (GET, POST, etc.)
.with_path() // Request path
.with_status() // Response status code
.with_duration() // Request duration in seconds
.with_size() // Response size in bytes
.with_remote_addr() // Client IP address
.with_request_id("request-id") // Auto-generated request ID
.build()
);
Exclude specific paths from logging:
let logger = SLogger::default()
.exclude("/health")
.exclude("/metrics");
Or use regex patterns:
let logger = SLogger::default()
.exclude_regex(r"^/assets/.*");
Change the logger target name:
let logger = SLogger::default().log_target("api.access");
The following fields can be added to your log output:
method - HTTP method (GET, POST, etc.)status - Response status codepath - Request pathparams - Query parametersversion - HTTP protocol versionhost - Request hostremote_addr - Client IP addressreal_ip - Client real IP (when behind proxy)request_id - Auto-generated or extracted request IDsize - Response size in bytesduration - Request duration in secondsduration_millis - Request duration in millisecondsdatetime - Timestamp in RFC3339 formatuser_agent - Client user agentreferer - Request referrerYou can also log custom request headers, response headers, and environment variables.
log (default) - Enable integration with the standard log cratetracing-request-id - Enable integration with tracing-actix-web's request IDuuid_v7 - Use UUIDv7 instead of UUIDv4 for request IDsThis project is licensed under the MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.