Skip to content

Commit

Permalink
fix: add check that the readme sample cfg is accepted by log4rs (estk…
Browse files Browse the repository at this point in the history
…#339)

* fix: add check that the readme sample cfg is accepted by log4rs

* fix: add new create_raw_config function to allow for usage in tests

* chore: self review cleanup
  • Loading branch information
bconn98 committed Jan 24, 2024
1 parent c1e41bc commit 1a771e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Cargo.lock
*.iml
.vscode/
info.log
log/

16 changes: 12 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ pub fn init_config_with_err_handler(
log::set_boxed_logger(Box::new(logger)).map(|()| handle)
}

/// Initializes the global logger as a log4rs logger using the provided raw config.
/// Create a log4rs logger using the provided raw config.
///
/// This will return errors if the appenders configuration is malformed or if we fail to set the global logger.
/// This will return errors if the appenders configuration is malformed.
#[cfg(feature = "config_parsing")]
pub fn init_raw_config(config: RawConfig) -> Result<(), InitError> {
pub fn create_raw_config(config: RawConfig) -> Result<crate::Logger, InitError> {
let (appenders, errors) = config.appenders_lossy(&Deserializers::default());
if !errors.is_empty() {
return Err(InitError::Deserializing(errors));
Expand All @@ -63,7 +63,15 @@ pub fn init_raw_config(config: RawConfig) -> Result<(), InitError> {
.loggers(config.loggers())
.build(config.root())?;

let logger = crate::Logger::new(config);
Ok(crate::Logger::new(config))
}

/// Initializes the global logger as a log4rs logger using the provided raw config.
///
/// This will return errors if the appenders configuration is malformed or if we fail to set the global logger.
#[cfg(feature = "config_parsing")]
pub fn init_raw_config(config: RawConfig) -> Result<(), InitError> {
let logger = create_raw_config(config)?;
log::set_max_level(logger.max_log_level());
log::set_boxed_logger(Box::new(logger))?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/config/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ loggers:
let config_str = sample_file[config_start..config_end].trim();

let config = ::serde_yaml::from_str::<RawConfig>(config_str);
assert!(config.is_ok())
assert!(config.is_ok());
assert!(config::create_raw_config(config.unwrap()).is_ok());
}
}

0 comments on commit 1a771e2

Please sign in to comment.