Implement configurable logging with tracing

- Replace eprintln! with tracing macros for configurable log levels
- Set up tracing_subscriber with level filtering from config
- Log format: YYYY-MM-DDTHH:MM:SSZ LEVEL IP "request" STATUS "message"
- Success logs: INFO level for 20 responses
- Error logs: WARN for 41/51, ERROR for 59
- Rate limiting and file serving now properly logged
- Remove unused RequestLogger::log_success method
This commit is contained in:
Jeena 2026-01-16 11:19:20 +00:00
parent 33ae576b25
commit 3865211554
3 changed files with 50 additions and 13 deletions

View file

@ -16,12 +16,20 @@ impl RequestLogger {
}
}
pub fn log_success(self, status_code: u8) {
println!("{} \"{}\" {}", self.client_ip, self.request_url, status_code);
}
pub fn log_error(self, status_code: u8, error_message: &str) {
eprintln!("{} \"{}\" {} \"{}\"", self.client_ip, self.request_url, status_code, error_message);
let level = match status_code {
41 | 51 => tracing::Level::WARN,
59 => tracing::Level::ERROR,
_ => tracing::Level::ERROR,
};
match level {
tracing::Level::WARN => tracing::warn!("{} \"{}\" {} \"{}\"", self.client_ip, self.request_url, status_code, error_message),
tracing::Level::ERROR => tracing::error!("{} \"{}\" {} \"{}\"", self.client_ip, self.request_url, status_code, error_message),
_ => {}
}
}
@ -35,8 +43,28 @@ fn extract_client_ip(stream: &TlsStream<TcpStream>) -> String {
}
}
pub fn init_logging(_level: &str) {
// Simple logging using stdout/stderr - systemd will capture this
pub fn init_logging(level: &str) {
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
let level = match level.to_lowercase().as_str() {
"error" => tracing::Level::ERROR,
"warn" => tracing::Level::WARN,
"info" => tracing::Level::INFO,
"debug" => tracing::Level::DEBUG,
"trace" => tracing::Level::TRACE,
_ => {
eprintln!("Warning: Invalid log level '{}', defaulting to 'info'", level);
tracing::Level::INFO
}
};
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer()
.compact()
.with_target(false)
.with_thread_ids(false))
.with(tracing_subscriber::filter::LevelFilter::from_level(level))
.init();
}
#[cfg(test)]