1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! All things pertaining to log4rs config.
#![doc = include_str!("../../docs/Configuration.md")]

use log::SetLoggerError;
use thiserror::Error;

use crate::Handle;

pub mod runtime;

#[cfg(feature = "config_parsing")]
mod file;
#[cfg(feature = "config_parsing")]
mod raw;

pub use runtime::{Appender, Config, Logger, Root};

#[cfg(feature = "config_parsing")]
pub use self::file::{init_file, load_config_file, FormatError};
#[cfg(feature = "config_parsing")]
pub use self::raw::{Deserializable, Deserialize, Deserializers, RawConfig};

/// Initializes the global logger as a log4rs logger with the provided config.
///
/// A `Handle` object is returned which can be used to adjust the logging
/// configuration.
pub fn init_config(config: runtime::Config) -> Result<crate::Handle, SetLoggerError> {
    let logger = crate::Logger::new(config);
    log::set_max_level(logger.max_log_level());
    let handle = Handle {
        shared: logger.0.clone(),
    };
    log::set_boxed_logger(Box::new(logger)).map(|()| handle)
}

/// Initializes the global logger as a log4rs logger with the provided config and error handler.
///
/// A `Handle` object is returned which can be used to adjust the logging
/// configuration.
pub fn init_config_with_err_handler(
    config: runtime::Config,
    err_handler: Box<dyn Send + Sync + Fn(&anyhow::Error)>,
) -> Result<crate::Handle, SetLoggerError> {
    let logger = crate::Logger::new_with_err_handler(config, err_handler);
    log::set_max_level(logger.max_log_level());
    let handle = Handle {
        shared: logger.0.clone(),
    };
    log::set_boxed_logger(Box::new(logger)).map(|()| handle)
}

/// Create a log4rs logger using the provided raw config.
///
/// This will return errors if the appenders configuration is malformed.
#[cfg(feature = "config_parsing")]
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));
    }
    let config = Config::builder()
        .appenders(appenders)
        .loggers(config.loggers())
        .build(config.root())?;

    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(())
}

/// Errors found when initializing.
#[derive(Debug, Error)]
pub enum InitError {
    /// There was an error deserializing.
    #[error("Errors found when deserializing the config: {0:#?}")]
    #[cfg(feature = "config_parsing")]
    Deserializing(#[from] raw::AppenderErrors),

    /// There was an error building the handle.
    #[error("Config building errors: {0:#?}")]
    BuildConfig(#[from] runtime::ConfigErrors),

    /// There was an error setting the global logger.
    #[error("Error setting the logger: {0:#?}")]
    SetLogger(#[from] log::SetLoggerError),
}