Expand description
Actix-web middleware for structured access logs.
This middleware inspired by the actix-web
’s Logger
middleware.
§Examples:
§By default middleware uses the standard log
crate for logging.
crate add actix-web-middleware-slogger
Example usage with standard log
crate and structured_logger
crate:
use actix_web::{web, App, HttpServer, main};
use actix_web_middleware_slogger::SLogger;
use tokio;
use structured_logger::{Builder, async_json::new_writer, unix_ms};
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
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")?;
Ok(())
}
§tracing-request-id
feature allows to log Request ID that set by TracingLogger
.
crate add actix-web-middleware-slogger --features tracing-request-id
Example usage with tracing-request-id
feature:
use actix_web;
use actix_web::{web, App, HttpServer};
use actix_web_middleware_slogger::SLogger;
use tokio;
use structured_logger::{Builder, async_json::new_writer, unix_ms};
use tracing_actix_web::TracingLogger;
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
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")?;
Ok(())
}
§Features
- Structured logging of HTTP requests and responses
- Fields selection (method, path, duration, headers, etc.)
- Support for standard
log
crate integration - Request ID tracking (with UUID v4 or v7 support)
- Integration with tracing ecosystem via
tracing-request-id
feature - Pattern-based path exclusion
§Configuration
§Custom Fields
You can customize which fields are included in your logs:
use actix_web_middleware_slogger::{SLogger, Fields};
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()
);
§Path Exclusions
Exclude specific paths from logging:
use actix_web_middleware_slogger::SLogger;
let logger = SLogger::default()
.exclude("/health")
.exclude("/metrics");
use regex patterns:
use actix_web_middleware_slogger::SLogger;
let logger = SLogger::default()
.exclude_regex(r"^/assets/.*");
§Available Fields
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 referrer
You can also log custom request headers, response headers, and environment variables.
§Feature Flags
log
(default) - Enable integration with the standardlog
cratetracing-request-id
- Enable integration withtracing-actix-web
’s request IDuuid_v7
- Use UUIDv7 instead of UUIDv4 for request IDs