diff --git a/Cargo.lock b/Cargo.lock index 4e18976..ef4ca78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,6 +146,7 @@ dependencies = [ "hyper", "hyper-util", "instant-acme", + "inventory", "jsonwebtoken", "k8s-openapi", "kube", @@ -1799,6 +1800,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "inventory" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4f0c30c76f2f4ccee3fe55a2435f691ca00c0e4bd87abe4f4a851b1d4dac39b" +dependencies = [ + "rustversion", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" diff --git a/Cargo.toml b/Cargo.toml index 823c643..d5e7b08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,3 +65,4 @@ k8s-openapi = { version = "0.28.0", features = ["v1_32"] } futures = "0.3.34" lazy_static = "1.5.0" anyhow = "1.0.103" +inventory = "0.3.24" diff --git a/src/logging/elasticsearch.rs b/src/logging/elasticsearch.rs index 3787870..f8bc667 100644 --- a/src/logging/elasticsearch.rs +++ b/src/logging/elasticsearch.rs @@ -1,5 +1,5 @@ use crate::logging::core::StructuredSystemLog; -use crate::logging::types::WriteLog; +use crate::logging::types::{LogBackendPlugin, WriteLog}; use std::env; use std::sync::LazyLock; @@ -28,7 +28,15 @@ pub struct ElasticSearch; impl WriteLog for ElasticSearch { fn writelog(&self, msg: &StructuredSystemLog) { if let Ok(jsonmsg) = serde_json::to_string(&msg) { - println!("Connecting to {} - {}:{} => {}", ELASTIC.hosts, ELASTIC.user, ELASTIC.password, jsonmsg); + // ElasticSearch logic is not yet implemented. + println!("{} - {}:{} => {}", ELASTIC.hosts, ELASTIC.user, ELASTIC.password, jsonmsg); } } } + +inventory::submit! { + LogBackendPlugin { + name: "elasticsearch", + factory: || Box::new(ElasticSearch), + } +} diff --git a/src/logging/example.rs b/src/logging/example.rs new file mode 100644 index 0000000..d978ef9 --- /dev/null +++ b/src/logging/example.rs @@ -0,0 +1,19 @@ +use crate::logging::core::StructuredSystemLog; +use crate::logging::types::{LogBackendPlugin, WriteLog}; + +pub struct Example; + +impl WriteLog for Example { + fn writelog(&self, msg: &StructuredSystemLog) { + // Here comes the backend logic + println!("Sending log Example : {:?}", msg); + } +} + +// Mandatory with name: matching value of "log_structired" in main.yaml +inventory::submit! { + LogBackendPlugin { + name: "example", + factory: || Box::new(Example), + } +} diff --git a/src/logging/mod.rs b/src/logging/mod.rs index 8906b7d..11b20af 100644 --- a/src/logging/mod.rs +++ b/src/logging/mod.rs @@ -1,3 +1,4 @@ pub mod core; pub mod elasticsearch; +pub mod example; pub mod types; diff --git a/src/logging/types.rs b/src/logging/types.rs index 08dcac4..2fbaee3 100644 --- a/src/logging/types.rs +++ b/src/logging/types.rs @@ -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, +} + +inventory::collect!(LogBackendPlugin); + +static BACKENDS: LazyLock>> = LazyLock::new(|| { + let mut map = HashMap::new(); + for plugin in inventory::iter:: { + 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); } }