Skip to content

Commit

Permalink
fix: add check that the readme sample cfg is accepted by log4rs (#339)
Browse files Browse the repository at this point in the history
* 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 23, 2024
1 parent 58b92c8 commit a69fdf9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
.idea/
*.iml
.vscode/
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 @@ -525,6 +525,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 a69fdf9

Please sign in to comment.