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:
parent
33ae576b25
commit
3865211554
3 changed files with 50 additions and 13 deletions
|
|
@ -17,10 +17,17 @@ pub async fn serve_file(
|
|||
) -> io::Result<()> {
|
||||
if file_path.exists() && file_path.is_file() {
|
||||
let mime_type = get_mime_type(file_path);
|
||||
let header = format!("20 {}\r\n", mime_type);
|
||||
stream.write_all(header.as_bytes()).await?;
|
||||
// Log success after sending header
|
||||
let client_ip = match stream.get_ref().0.peer_addr() {
|
||||
Ok(addr) => addr.to_string(),
|
||||
Err(_) => "unknown".to_string(),
|
||||
};
|
||||
tracing::info!("{} \"file:{}\" 20 \"Success\"", client_ip, file_path.display());
|
||||
// Then send body
|
||||
let content = fs::read(file_path)?;
|
||||
let mut response = format!("20 {}\r\n", mime_type).into_bytes();
|
||||
response.extend(content);
|
||||
stream.write_all(&response).await?;
|
||||
stream.write_all(&content).await?;
|
||||
stream.flush().await?;
|
||||
Ok(())
|
||||
} else {
|
||||
|
|
@ -118,10 +125,12 @@ pub async fn handle_connection(
|
|||
|
||||
// Serve the file
|
||||
match serve_file(&mut stream, &file_path).await {
|
||||
Ok(_) => logger.log_success(20),
|
||||
Ok(_) => {
|
||||
// Success already logged in serve_file
|
||||
}
|
||||
Err(_) => {
|
||||
// This shouldn't happen since we check existence, but handle gracefully
|
||||
logger.log_error(51, "File not found");
|
||||
// File transmission failed
|
||||
logger.log_error(51, "File transmission failed");
|
||||
let _ = send_response(&mut stream, "51 Not found\r\n").await;
|
||||
}
|
||||
}
|
||||
|
|
@ -155,7 +164,6 @@ pub async fn handle_connection(
|
|||
}
|
||||
|
||||
ACTIVE_REQUESTS.fetch_sub(1, Ordering::Relaxed);
|
||||
eprintln!("DEBUG: Request completed, count decremented");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue