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:
parent
3865211554
commit
051157a84c
3 changed files with 22 additions and 4 deletions
|
|
@ -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"
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ static ACTIVE_REQUESTS: AtomicUsize = AtomicUsize::new(0);
|
|||
pub async fn serve_file(
|
||||
stream: &mut TlsStream<TcpStream>,
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue