actix_web_middleware_slogger/
lib.rs

1//! Actix-web middleware for structured access logs.
2//! This middleware inspired by the `actix-web`'s `Logger` middleware.
3//!
4//! # Examples:
5//! ## By default middleware uses the standard `log` crate for logging.
6//! ```bash
7//! crate add actix-web-middleware-slogger
8//! ```
9//! Example usage with standard `log` crate and `structured_logger` crate:
10//! ```rust
11//! use actix_web::{web, App, HttpServer, main};
12//! use actix_web_middleware_slogger::SLogger;
13//! use tokio;
14//! use structured_logger::{Builder, async_json::new_writer, unix_ms};
15//!
16//! #[actix_web::main] // or #[tokio::main]
17//! async fn main() -> std::io::Result<()> {
18//!     Builder::new()
19//!         .with_target_writer("*", new_writer(tokio::io::stdout()))
20//!         .init();
21//!
22//!     HttpServer::new(|| {
23//!         App::new()
24//!             .wrap(SLogger::default())
25//!             .route("/", web::get().to(|| async { "Hello world!" }))
26//!     })
27//!     .bind("127.0.0.1:8080")?;
28//!     Ok(())
29//! }
30//! ```
31//! ## `tracing-request-id` feature allows to log Request ID that set by `TracingLogger`.
32//! ```bash
33//! crate add actix-web-middleware-slogger --features tracing-request-id
34//! ```
35//! Example usage with `tracing-request-id` feature:
36//! ```rust
37//! use actix_web;
38//! use actix_web::{web, App, HttpServer};
39//! use actix_web_middleware_slogger::SLogger;
40//! use tokio;
41//! use structured_logger::{Builder, async_json::new_writer, unix_ms};
42//! use tracing_actix_web::TracingLogger;
43//!
44//! #[actix_web::main] // or #[tokio::main]
45//! async fn main() -> std::io::Result<()> {
46//!     Builder::new()
47//!         .with_target_writer("*", new_writer(tokio::io::stdout()))
48//!         .init();
49//!
50//!     HttpServer::new(|| {
51//!         App::new()
52//!             .wrap(TracingLogger::default())
53//!             .wrap(SLogger::default())
54//!             .route("/", web::get().to(|| async { "Hello world!" }))
55//!     })
56//!     .bind("127.0.0.1:8080")?;
57//!     Ok(())
58//! }
59//!```
60//! # Features
61//! - Structured logging of HTTP requests and responses
62//! - Fields selection (method, path, duration, headers, etc.)
63//! - Support for standard `log` crate integration
64//! - Request ID tracking (with UUID v4 or v7 support)
65//! - Integration with tracing ecosystem via `tracing-request-id` feature
66//! - Pattern-based path exclusion
67//!
68//! # Configuration
69//!
70//! ## Custom Fields
71//!
72//! You can customize which fields are included in your logs:
73//!
74//! ```rust
75//! use actix_web_middleware_slogger::{SLogger, Fields};
76//!
77//! let logger = SLogger::new(
78//!     Fields::builder()
79//!         .with_method()                  // HTTP method (GET, POST, etc.)
80//!         .with_path()                    // Request path
81//!         .with_status()                  // Response status code
82//!         .with_duration()                // Request duration in seconds
83//!         .with_size()                    // Response size in bytes
84//!         .with_remote_addr()             // Client IP address
85//!         .with_request_id("request-id")  // Auto-generated request ID
86//!         .build()
87//! );
88//! ```
89//! ## Path Exclusions
90//!
91//! Exclude specific paths from logging:
92//!
93//! ```rust
94//! use actix_web_middleware_slogger::SLogger;
95//!
96//! let logger = SLogger::default()
97//!     .exclude("/health")
98//!     .exclude("/metrics");
99//! ```
100//!
101//! use regex patterns:
102//!
103//! ```rust
104//! use actix_web_middleware_slogger::SLogger;
105//!
106//! let logger = SLogger::default()
107//!     .exclude_regex(r"^/assets/.*");
108//! ```
109//! # Available Fields
110//!
111//! The following fields can be added to your log output:
112//!
113//! - `method` - HTTP method (GET, POST, etc.)
114//! - `status` - Response status code
115//! - `path` - Request path
116//! - `params` - Query parameters
117//! - `version` - HTTP protocol version
118//! - `host` - Request host
119//! - `remote_addr` - Client IP address
120//! - `real_ip` - Client real IP (when behind proxy)
121//! - `request_id` - Auto-generated or extracted request ID
122//! - `size` - Response size in bytes
123//! - `duration` - Request duration in seconds
124//! - `duration_millis` - Request duration in milliseconds
125//! - `datetime` - Timestamp in RFC3339 format
126//! - `user_agent` - Client user agent
127//! - `referer` - Request referrer
128//!
129//! You can also log custom request headers, response headers, and environment variables.
130//!
131//! # Feature Flags
132//!
133//! - `log` (default) - Enable integration with the standard `log` crate
134//! - `tracing-request-id` - Enable integration with `tracing-actix-web`'s request ID
135//! - `uuid_v7` - Use UUIDv7 instead of UUIDv4 for request IDs
136
137mod logger;
138mod wrapper;
139
140pub use crate::logger::RequestId;
141pub use crate::logger::{Fields, SLogger};
142pub use crate::wrapper::rust_log;