From 0b6f482e5122a6bad469d58c421d20e85f97a20a Mon Sep 17 00:00:00 2001 From: Thomas Wickham Date: Tue, 2 Mar 2021 20:51:33 +0100 Subject: [PATCH] Add logging + start wiring config --- Cargo.toml | 2 +- src/infra/cli.rs | 12 ++++++++++-- src/infra/configuration.rs | 27 +++++++++++++++++---------- src/infra/logging.rs | 17 +++++++++++++++++ src/infra/mod.rs | 1 + src/main.rs | 15 ++++++++++++--- 6 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 src/infra/logging.rs diff --git a/Cargo.toml b/Cargo.toml index 70936e6..b872e8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ actix-web = "3" anyhow = "*" clap = "3.0.0-beta.2" http = "*" -passablewords = "*" +log = "*" serde = "*" thiserror = "*" tracing = "*" diff --git a/src/infra/cli.rs b/src/infra/cli.rs index 049ee67..43bfd9b 100644 --- a/src/infra/cli.rs +++ b/src/infra/cli.rs @@ -1,9 +1,17 @@ use clap::Clap; /// lldap is a lightweight LDAP server -#[derive(Debug, Clap)] +#[derive(Debug, Clap, Clone)] #[clap(version = "0.1", author = "The LLDAP team")] -pub struct CLIOpts; +pub struct CLIOpts { + /// Change config file name + #[clap(short, long, default_value = "lldap_config.toml")] + pub config_file: String, + + /// Set verbose logging + #[clap(short, long)] + pub verbose: bool, +} pub fn init() -> CLIOpts { CLIOpts::parse() diff --git a/src/infra/configuration.rs b/src/infra/configuration.rs index 6e2ce0a..088ac2e 100644 --- a/src/infra/configuration.rs +++ b/src/infra/configuration.rs @@ -5,26 +5,33 @@ use figment::{ }; use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize, Serialize)] +use crate::infra::cli::CLIOpts; + +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Configuration { - secret_pepper: String, - some_text: String, + pub log_level_verbose: bool, + pub secret_pepper: String, + pub some_text: String, } -impl Default for Configuration { - fn default() -> Self { +impl Configuration { + fn from_cli(cli_opts: CLIOpts) -> Self { Configuration { + log_level_verbose: cli_opts.verbose, secret_pepper: String::from("secretsecretpepper"), some_text: String::new(), } } } -pub fn init() -> Result { - let config: Configuration = Figment::from(Serialized::defaults(Configuration::default())) - .merge(Toml::file("lldap_config.toml")) - .merge(Env::prefixed("LLDAP_")) - .extract()?; +pub fn init(cli_opts: CLIOpts) -> Result { + // FIXME cli arguments are less prioritary than toml config file or env... Not good. + let config: Configuration = Figment::from(Serialized::defaults(Configuration::from_cli( + cli_opts.clone(), + ))) + .merge(Toml::file(cli_opts.config_file)) + .merge(Env::prefixed("LLDAP_")) + .extract()?; Ok(config) } diff --git a/src/infra/logging.rs b/src/infra/logging.rs new file mode 100644 index 0000000..83204af --- /dev/null +++ b/src/infra/logging.rs @@ -0,0 +1,17 @@ +use crate::infra::configuration::Configuration; +use anyhow::Context; +use tracing::subscriber::set_global_default; +use tracing_log::LogTracer; + +pub fn init(_config: Configuration) -> anyhow::Result<()> { + // TODO: use config.log_level_verbose to set level + + let subscriber = tracing_subscriber::fmt() + .with_timer(tracing_subscriber::fmt::time::time()) + .with_target(false) + .with_level(true) + .finish(); + LogTracer::init().context("Failed to set logger")?; + set_global_default(subscriber).context("Failed to set subscriber")?; + Ok(()) +} diff --git a/src/infra/mod.rs b/src/infra/mod.rs index d01d024..a0eba58 100644 --- a/src/infra/mod.rs +++ b/src/infra/mod.rs @@ -1,2 +1,3 @@ pub mod cli; pub mod configuration; +pub mod logging; diff --git a/src/main.rs b/src/main.rs index d41b24a..a42b692 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,16 @@ +use anyhow::Result; +use log::*; + mod infra; -fn main() { - let config = infra::configuration::init(); +fn main() -> Result<()> { let cli_opts = infra::cli::init(); - println!("Hello, world! Config: {:?}, CLI: {:?}", config, cli_opts); + let config = infra::configuration::init(cli_opts.clone())?; + infra::logging::init(config.clone())?; + + info!("Starting...."); + debug!("Config: {:?}", config); + debug!("CLI: {:?}", cli_opts); + info!("End."); + Ok(()) }