micro_kit/
logging.rs

1use ::yaml::{YamlEmitter};
2use ::config::{ConfigFile, ConfigError};
3use log4rs;
4
5pub use log4rs::init_config;
6
7#[derive(Debug)]
8pub struct LoggingConfig;
9
10impl LoggingConfig {
11
12    pub fn config_logging(config: &ConfigFile) -> Result<log4rs::config::Config, ConfigError> {
13        if !config["logging"].is_badvalue() {
14            let mut log_config_buf = String::new();
15            { //Block because of the mutable borrow, keep the mutability in this scope.
16                let mut log_emitter = YamlEmitter::new(&mut log_config_buf);
17                log_emitter.dump(&config["logging"])?;
18            }
19            let log_config_str = log_config_buf.as_str();
20            let log_config = log4rs::file::Config::parse(log_config_str,
21                                                         log4rs::file::Format::Yaml,
22                                                         &log4rs::file::Deserializers::default())?;
23            Ok(log_config.into_config())
24        } else {
25            Err(ConfigError::MissingComponent("logging".to_string()))
26        }
27    }
28}