added inventory for immutable core logging files

This commit is contained in:
Ara Sadoyan
2026-09-22 18:48:42 +02:00
parent 639365c43b
commit 54ac35a4c9
6 changed files with 64 additions and 8 deletions
+23 -6
View File
@@ -1,13 +1,30 @@
use crate::logging::core::StructuredSystemLog;
use crate::logging::elasticsearch::ElasticSearch;
use std::collections::HashMap;
use std::sync::LazyLock;
pub trait WriteLog {
pub trait WriteLog: Send + Sync {
fn writelog(&self, msg: &StructuredSystemLog);
}
pub struct LogBackendPlugin {
pub name: &'static str,
pub factory: fn() -> Box<dyn WriteLog>,
}
inventory::collect!(LogBackendPlugin);
static BACKENDS: LazyLock<HashMap<&'static str, Box<dyn WriteLog>>> = LazyLock::new(|| {
let mut map = HashMap::new();
for plugin in inventory::iter::<LogBackendPlugin> {
map.insert(plugin.name, (plugin.factory)());
}
map
});
pub fn sendlog(backend: &str, msg: &StructuredSystemLog) {
match backend {
"elasticsearch" => ElasticSearch.writelog(msg),
"somewhereelse" => println!("somewhereelse"),
_ => log::warn!("Unsupported logging mechanism : {}", backend),
if let Some(logger) = BACKENDS.get(backend) {
logger.writelog(msg);
} else {
log::warn!("Unsupported logging mechanism: {}", backend);
}
}