This commit is contained in:
Mateusz Gruszczyński committed 2026-10-09 17:09:41 +02:00
1 parent 3e33982cd3
commit ad95bfb050
27 files changed
+309 -80

No files matched your search

+50
View File
@@ -1,5 +1,6 @@
mod tests {
use super::*;
use tracing_subscriber::prelude::*;
fn entry() -> LogEntry {
LogEntry {
@@ -212,6 +213,7 @@ mod tests {
fn error_json_is_one_record_and_reserved_fields_cannot_be_overwritten() {
let event = ErrorEvent {
timestamp: "2026-10-03T12:34:56.123Z".into(),
level: "ERROR".into(),
target: "rust_proxy::http".into(),
message: "upstream failed\nsecond line".into(),
fields: BTreeMap::from([
@@ -224,6 +226,54 @@ mod tests {
let value: Value = serde_json::from_slice(&line).unwrap();
assert_eq!(value["message"], "upstream failed\nsecond line");
assert_eq!(value["request_id"], "abc");
assert_eq!(value["level"], "ERROR");
}
#[tokio::test]
async fn diagnostics_switch_between_console_and_file_without_restart() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("error.log");
let controller = ErrorLogController::start();
assert!(controller.console_enabled_for(&Level::WARN));
assert!(controller.console_enabled_for(&Level::ERROR));
controller.configure(ErrorLogSettings {
enabled: true,
path: path.to_string_lossy().into_owned(),
format: ErrorLogFormat::Json,
});
assert!(!controller.console_enabled_for(&Level::WARN));
assert!(!controller.console_enabled_for(&Level::ERROR));
assert!(controller.console_enabled_for(&Level::INFO));
let subscriber = tracing_subscriber::registry().with(ErrorLogLayer::new(controller.clone()));
tracing::subscriber::with_default(subscriber, || {
tracing::warn!(target: "rust_proxy::tests", "PHP-FPM FastCGI stderr");
tracing::error!(target: "rust_proxy::tests", "RustProxy worker panicked");
});
let content = tokio::time::timeout(Duration::from_secs(5), async {
loop {
if let Ok(content) = tokio::fs::read_to_string(&path).await {
if content.lines().count() == 2 {
break content;
}
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
})
.await
.expect("diagnostics were not written to the configured error file");
let entries: Vec<Value> = content
.lines()
.map(|line| serde_json::from_str(line).unwrap())
.collect();
assert_eq!(entries[0]["level"], "WARN");
assert_eq!(entries[1]["level"], "ERROR");
controller.configure(ErrorLogSettings::default());
assert!(controller.console_enabled_for(&Level::WARN));
assert!(controller.console_enabled_for(&Level::ERROR));
}
#[test]