From 051157a84cc1b08731668015f13eadd1385fb88b Mon Sep 17 00:00:00 2001 From: Jeena Date: Fri, 16 Jan 2026 11:34:38 +0000 Subject: [PATCH] Fix logging format: use request path instead of file path, clean timestamp - Log request paths (/big-file.mkv) instead of file system paths - Custom timestamp format: YYYY-MM-DDTHH:MM:SSZ (no milliseconds) - Update serve_file to accept request parameter for proper logging - Strip gemini://host prefix from logged requests for cleaner logs - Add time crate for custom timestamp formatting --- Cargo.toml | 1 + src/logging.rs | 19 +++++++++++++++++-- src/server.rs | 6 ++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8c7efc7..d3db4c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ toml = "0.8" serde = { version = "1.0", features = ["derive"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "ansi"] } +time = "0.3" [dev-dependencies] tempfile = "3" \ No newline at end of file diff --git a/src/logging.rs b/src/logging.rs index 4f66ffd..ab2bd31 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,5 +1,17 @@ use tokio::net::TcpStream; use tokio_rustls::server::TlsStream; +use tracing_subscriber::fmt::time::FormatTime; + +struct GeminiTimeFormat; + +impl FormatTime for GeminiTimeFormat { + fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result { + let now = time::OffsetDateTime::now_utc(); + write!(w, "{}-{:02}-{:02}T{:02}:{:02}:{:02}Z", + now.year(), now.month() as u8, now.day(), + now.hour(), now.minute(), now.second()) + } +} pub struct RequestLogger { client_ip: String, @@ -25,9 +37,11 @@ impl RequestLogger { _ => tracing::Level::ERROR, }; + let request_path = self.request_url.strip_prefix("gemini://localhost").unwrap_or(&self.request_url); + 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), + tracing::Level::WARN => tracing::warn!("{} \"{}\" {} \"{}\"", self.client_ip, request_path, status_code, error_message), + tracing::Level::ERROR => tracing::error!("{} \"{}\" {} \"{}\"", self.client_ip, request_path, status_code, error_message), _ => {} } } @@ -61,6 +75,7 @@ pub fn init_logging(level: &str) { tracing_subscriber::registry() .with(tracing_subscriber::fmt::layer() .compact() + .with_timer(GeminiTimeFormat) .with_target(false) .with_thread_ids(false)) .with(tracing_subscriber::filter::LevelFilter::from_level(level)) diff --git a/src/server.rs b/src/server.rs index d1b744b..d98e311 100644 --- a/src/server.rs +++ b/src/server.rs @@ -14,6 +14,7 @@ static ACTIVE_REQUESTS: AtomicUsize = AtomicUsize::new(0); pub async fn serve_file( stream: &mut TlsStream, file_path: &Path, + request: &str, ) -> io::Result<()> { if file_path.exists() && file_path.is_file() { let mime_type = get_mime_type(file_path); @@ -24,7 +25,8 @@ pub async fn serve_file( Ok(addr) => addr.to_string(), Err(_) => "unknown".to_string(), }; - tracing::info!("{} \"file:{}\" 20 \"Success\"", client_ip, file_path.display()); + let request_path = request.strip_prefix("gemini://localhost").unwrap_or(request); + tracing::info!("{} \"{}\" 20 \"Success\"", client_ip, request_path); // Then send body let content = fs::read(file_path)?; stream.write_all(&content).await?; @@ -124,7 +126,7 @@ pub async fn handle_connection( // Processing complete // Serve the file - match serve_file(&mut stream, &file_path).await { + match serve_file(&mut stream, &file_path, &request).await { Ok(_) => { // Success already logged in serve_file }