This commit is contained in:
2026-01-06 10:10:36 +01:00
commit 6a0f1771f6
15 changed files with 3295 additions and 0 deletions

47
src/logs.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::{
fs::{self, OpenOptions},
io::Write,
time::Instant,
};
use actix_web::HttpRequest;
use crate::LOG_DIR;
pub fn log_to_file(req: &HttpRequest, start: Instant) {
let delta = start.elapsed().as_nanos();
println!("Request processed in {} ns", delta);
let duration_ms = delta as f64 / 1000_000.0;
let _ = fs::create_dir_all(LOG_DIR);
let log_path = LOG_DIR.to_string() + "access.log";
let Ok(mut file) = OpenOptions::new().create(true).append(true).open(log_path) else {
eprintln!("failed to open log file");
return;
};
let ts = chrono::Local::now().to_rfc3339();
let method = req.method();
let uri = req.uri();
let path = uri.path();
let query = uri.query().unwrap_or("-");
let connection_info = req.connection_info();
let scheme = connection_info.scheme();
let ip = connection_info.peer_addr().unwrap_or("-");
let real_ip = connection_info.realip_remote_addr().unwrap_or("-");
let ua = req
.headers()
.get("user-agent")
.and_then(|v| v.to_str().ok())
.unwrap_or("-");
let line = format!(
"{ts} scheme={scheme} ip={ip} real_ip={real_ip} method={method} path={path} qs={query} dur_ms={duration_ms} ua=\"{ua}\"\n"
);
let _ = file.write_all(line.as_bytes());
}