Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
}
}