feat: JSON-Logging mit Correlation-ID, Anfrage-Log und Audit (M2.4)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 17:39:03 +01:00
parent 2ec9eccde6
commit 6cb930d0b0
31 changed files with 1050 additions and 67 deletions

View File

@ -16,6 +16,8 @@ pub struct Config {
pub listen: String,
/// Ordner für Datenbank, Logs und Ergebnisse.
pub data_dir: PathBuf,
/// Log-Stufe: `error`, `warn`, `info`, `debug` oder `trace`.
pub log_level: String,
/// Rohe Einstellungen des llama.cpp-Adapters (Abschnitt `[llama_cpp]`).
/// Dieses Crate kennt die Adapter nicht; der Adapter liest die Tabelle selbst.
pub llama_cpp: toml::Table,
@ -26,6 +28,7 @@ impl Default for Config {
Self {
listen: "127.0.0.1:8080".to_string(),
data_dir: PathBuf::from("data"),
log_level: "info".to_string(),
llama_cpp: toml::Table::new(),
}
}
@ -53,6 +56,12 @@ impl Config {
self.listen.parse::<SocketAddr>().map_err(|e| {
Error::Config(format!("listen \"{}\" ist keine Adresse: {e}", self.listen))
})?;
if !["error", "warn", "info", "debug", "trace"].contains(&self.log_level.as_str()) {
return Err(Error::Config(format!(
"log_level \"{}\" ist unbekannt (error, warn, info, debug, trace)",
self.log_level
)));
}
if self.data_dir.as_os_str().is_empty() {
return Err(Error::Config("data_dir darf nicht leer sein".to_string()));
}
@ -102,6 +111,13 @@ mod tests {
assert_eq!(c.llama_cpp["ready_timeout_secs"].as_integer(), Some(5));
}
#[test]
fn log_stufe_wird_geprueft() {
assert_eq!(Config::default().log_level, "info");
assert!(Config::from_toml("log_level = \"debug\"").is_ok());
assert!(Config::from_toml("log_level = \"laut\"").is_err());
}
#[test]
fn leeres_datenverzeichnis_wird_abgelehnt() {
assert!(Config::from_toml("data_dir = \"\"").is_err());

View File

@ -6,11 +6,13 @@ mod apikey;
mod config;
mod error;
mod priority;
mod time;
pub use apikey::{generate_api_key, hash_api_key, random_hex};
pub use config::Config;
pub use error::{Error, Result};
pub use priority::Priority;
pub use time::{iso8601_from_unix, now_iso8601};
/// Version des Nexus-Workspaces.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@ -0,0 +1,61 @@
//! UTC-Zeitstempel im Format ISO 8601 ohne zusätzliche Abhängigkeit.
use std::time::{SystemTime, UNIX_EPOCH};
/// Sekunden seit 1970 als `2026-11-09T16:20:00Z`.
pub fn iso8601_from_unix(secs: u64) -> String {
let days = (secs / 86_400) as i64;
let rest = secs % 86_400;
let (year, month, day) = civil_from_days(days);
format!(
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
rest / 3600,
rest % 3600 / 60,
rest % 60
)
}
/// Jetzt als `2026-11-09T16:20:00Z`.
pub fn now_iso8601() -> String {
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
iso8601_from_unix(secs)
}
/// Tage seit 1970-01-01 in (Jahr, Monat, Tag), Algorithmus von Howard Hinnant.
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
let year = yoe + era * 400 + i64::from(month <= 2);
(year, month, day)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bekannte_zeitpunkte() {
assert_eq!(iso8601_from_unix(0), "1970-01-01T00:00:00Z");
assert_eq!(iso8601_from_unix(1_700_000_000), "2023-11-14T22:13:20Z");
// Schalttag
assert_eq!(iso8601_from_unix(951_782_400), "2000-02-29T00:00:00Z");
assert_eq!(iso8601_from_unix(1_709_164_800), "2024-02-29T00:00:00Z");
assert_eq!(iso8601_from_unix(4_102_444_799), "2099-12-31T23:59:59Z");
}
#[test]
fn jetzt_hat_das_erwartete_format() {
let now = now_iso8601();
assert_eq!(now.len(), 20);
assert!(now.starts_with("20") && now.ends_with('Z'));
}
}