diff options
Diffstat (limited to 'src/logging.rs')
-rw-r--r-- | src/logging.rs | 78 |
1 files changed, 53 insertions, 25 deletions
diff --git a/src/logging.rs b/src/logging.rs index c97948f..15008be 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,36 +1,64 @@ -use crate::Result; use figment::Provider; -use std::{fs::File, sync::Arc}; +use std::{fs::File, ops::Deref, sync::Arc}; use tracing::metadata::LevelFilter; use tracing_subscriber::{prelude::*, Layer}; pub use config::Config; +pub use error::{Error, Result}; pub use level::Level; mod config; +mod error; mod level; -pub fn init_from_provider<T: Provider>(provider: T) -> Result<()> { - let config = Config::extract(&provider)?; - - let stdout_layer = tracing_subscriber::fmt::layer() - .pretty() - .with_filter(LevelFilter::from(config.level)); - - let log_layer = if config.level.is_some() { - let file = File::create(&config.path)?; - tracing_subscriber::fmt::layer() - .with_writer(Arc::new(file)) - .with_filter(LevelFilter::from(config.level)) - .into() - } else { - None - }; - - tracing_subscriber::registry() - .with(stdout_layer) - .with(log_layer) - .init(); - - Ok(()) +pub struct Logging(Config); + +impl Logging { + pub fn new() -> Result<Self> { + Self::from_provider(Config::figment()) + } + + /// Extract `Config` from `provider` to construct new `Finder` + pub fn from_provider<T: Provider>(provider: T) -> Result<Self> { + Config::extract(&provider) + .map_err(Into::into) + .map(Into::into) + } + + pub fn init(&self) -> Result<()> { + let stdout_layer = tracing_subscriber::fmt::layer() + .pretty() + .with_filter(LevelFilter::from(self.level)); + + let log_layer = if self.level.is_some() { + let file = File::create(&self.path)?; + tracing_subscriber::fmt::layer() + .with_writer(Arc::new(file)) + .with_filter(LevelFilter::from(self.level)) + .into() + } else { + None + }; + + tracing_subscriber::registry() + .with(stdout_layer) + .with(log_layer) + .init(); + + Ok(()) + } +} + +impl Deref for Logging { + type Target = Config; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From<Config> for Logging { + fn from(value: Config) -> Self { + Self(value) + } } |