1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! # HTTP Request and Response Logger Middleware
//!
//! This module provides comprehensive HTTP logging middleware for the Ignitia web framework.
//! It logs incoming requests and outgoing responses with detailed information about
//! HTTP methods, paths, versions, and status codes using the `tracing` crate.
//!
//! ## Features
//!
//! - **Request Logging**: Logs incoming HTTP requests with method, path, and HTTP version
//! - **Response Logging**: Logs outgoing responses with status codes
//! - **Structured Logging**: Uses the `tracing` crate for structured, level-based logging
//! - **Zero Configuration**: Works out of the box with sensible defaults
//! - **Performance Optimized**: Minimal overhead logging implementation
//!
//! ## Usage
//!
//! ### Basic Usage
//! ```
//! use ignitia::{Router, LoggerMiddleware};
//!
//! let router = Router::new()
//! .middleware(LoggerMiddleware)
//! .get("/", || async { Ok(ignitia::Response::text("Hello World!")) });
//! ```
//!
//! ### With Custom Logging Configuration
//! ```
//! use ignitia::{Router, LoggerMiddleware};
//! use tracing_subscriber;
//!
//! // Initialize tracing subscriber with custom format
//! tracing_subscriber::fmt()
//! .with_target(false)
//! .with_thread_ids(true)
//! .with_level(true)
//! .init();
//!
//! let router = Router::new()
//! .middleware(LoggerMiddleware)
//! .get("/api/users", || async { Ok(ignitia::Response::text("Users")) });
//! ```
//!
//! ## Log Format
//!
//! The middleware produces logs in the following format:
//!
//! ### Request Logs
//! ```
//! GET /api/users HTTP/1.1
//! POST /api/users HTTP/2.0
//! DELETE /api/users/123 HTTP/1.1
//! ```
//!
//! ### Response Logs
//! ```
//! Response: 200
//! Response: 404
//! Response: 500
//! ```
//!
//! ## HTTP Version Support
//!
//! The middleware correctly identifies and logs all HTTP versions:
//! - HTTP/0.9 (rarely used)
//! - HTTP/1.0
//! - HTTP/1.1 (most common)
//! - HTTP/2.0 (modern browsers and servers)
//! - HTTP/3.0 (latest standard)
//!
//! ## Integration with Tracing
//!
//! This middleware uses the `tracing` crate's `info!` macro, which means:
//! - Logs are structured and can be filtered by level
//! - Integration with distributed tracing systems is possible
//! - Custom formatting and output targets are supported
//!
//! ### Configuring Log Levels
//! ```
//! use tracing_subscriber;
//!
//! // Only show warnings and errors (hide info logs)
//! std::env::set_var("RUST_LOG", "warn");
//! tracing_subscriber::fmt::init();
//!
//! // Show all logs including debug
//! std::env::set_var("RUST_LOG", "debug");
//! tracing_subscriber::fmt::init();
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Minimal Overhead**: Logging operations are very fast
//! - **Non-Blocking**: Uses async logging that doesn't block request processing
//! - **Memory Efficient**: No significant memory allocation per request
//! - **CPU Efficient**: Simple string formatting with minimal processing
//!
//! ## Custom Logging Middleware
//!
//! If you need more advanced logging, you can create custom middleware:
//!
//! ```
//! use ignitia::{Middleware, Request, Response, Result};
//! use async_trait::async_trait;
//! use tracing::{info, warn};
//! use std::time::Instant;
//!
//! pub struct CustomLoggerMiddleware;
//!
//! #[async_trait]
//! impl Middleware for CustomLoggerMiddleware {
//! async fn before(&self, req: &mut Request) -> Result<()> {
//! let start_time = Instant::now();
//! req.insert_extension(start_time);
//!
//! info!(
//! method = %req.method,
//! path = req.uri.path(),
//! query = req.uri.query(),
//! user_agent = req.header("user-agent").unwrap_or("unknown"),
//! "Request started"
//! );
//! Ok(())
//! }
//!
//! async fn after(&self, res: &mut Response) -> Result<()> {
//! let status = res.status.as_u16();
//!
//! if status >= 400 {
//! warn!(status = status, "Request completed with error");
//! } else {
//! info!(status = status, "Request completed successfully");
//! }
//!
//! Ok(())
//! }
//! }
//! ```
//!
//! ## Production Considerations
//!
//! ### Log Rotation
//! For production use, consider implementing log rotation:
//! ```
//! use tracing_appender::rolling::{RollingFileAppender, Rotation};
//! use tracing_subscriber::fmt::writer::MakeWriterExt;
//!
//! let file_appender = RollingFileAppender::new(Rotation::DAILY, "/var/log/myapp", "app.log");
//! let (non_blocking_writer, _guard) = tracing_appender::non_blocking(file_appender);
//!
//! tracing_subscriber::fmt()
//! .with_writer(non_blocking_writer)
//! .init();
//! ```
//!
//! ### Security Considerations
//! - Be careful not to log sensitive data like authentication tokens
//! - Consider filtering or masking sensitive query parameters
//! - Ensure log files have appropriate permissions in production
use crateMiddleware;
use crate::;
use info;
use Next;
/// A simple HTTP request and response logging middleware.
///
/// This middleware logs incoming HTTP requests with their method, path, and HTTP version,
/// and outgoing responses with their status codes. It uses the `tracing` crate for
/// structured logging.
///
/// # Examples
///
/// ## Basic Usage
/// ```
/// use ignitia::{Router, LoggerMiddleware};
///
/// let router = Router::new()
/// .middleware(LoggerMiddleware)
/// .get("/", || async { Ok(ignitia::Response::text("Hello!")) });
/// ```
///
/// ## With Multiple Routes
/// ```
/// use ignitia::{Router, LoggerMiddleware, Response, Result};
///
/// let router = Router::new()
/// .middleware(LoggerMiddleware)
/// .get("/users", || async { Ok(Response::text("Users list")) })
/// .post("/users", || async { Ok(Response::text("User created")) })
/// .get("/health", || async { Ok(Response::text("OK")) });
/// ```
///
/// ## Expected Log Output
/// ```
/// INFO GET /users HTTP/1.1
/// INFO Response: 200
/// INFO POST /users HTTP/1.1
/// INFO Response: 201
/// INFO GET /health HTTP/1.1
/// INFO Response: 200
/// ```
;