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
This commit is contained in:
Jeena 2026-01-16 11:34:38 +00:00
parent 3865211554
commit 051157a84c
3 changed files with 22 additions and 4 deletions

View file

@ -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))