yeah works
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ErrorEntry {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub url: String,
|
||||
pub error_type: String,
|
||||
pub message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub status_code: Option<u16>,
|
||||
}
|
||||
|
||||
pub fn log_error(
|
||||
log_path: &Path,
|
||||
url: &Url,
|
||||
error_type: &str,
|
||||
message: &str,
|
||||
status_code: Option<u16>,
|
||||
) {
|
||||
let entry = ErrorEntry {
|
||||
timestamp: Utc::now(),
|
||||
url: url.to_string(),
|
||||
error_type: error_type.to_string(),
|
||||
message: message.to_string(),
|
||||
status_code,
|
||||
};
|
||||
|
||||
let line = serde_json::to_string(&entry).unwrap_or_default() + "\n";
|
||||
|
||||
if let Ok(mut file) = OpenOptions::new().append(true).create(true).open(log_path) {
|
||||
let _ = file.write_all(line.as_bytes());
|
||||
let _ = file.flush();
|
||||
}
|
||||
|
||||
println!(" LOGGED ERROR [{}]: {}", error_type, message);
|
||||
}
|
||||
Reference in New Issue
Block a user