refactor configs into a crate
This commit is contained in:
52
apps/api/src/configs/logging.rs
Normal file
52
apps/api/src/configs/logging.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use config::{Config, ConfigError};
|
||||
use tracing::{Level, warn};
|
||||
|
||||
use super::{
|
||||
FromConfig,
|
||||
key::{LOGGING_LEVEL_KEY, LOGGING_UTC_KEY},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LoggingSettings {
|
||||
pub level: Level,
|
||||
pub utc: bool,
|
||||
}
|
||||
|
||||
impl FromConfig for LoggingSettings {
|
||||
fn from_config(_config: &Config) -> Result<Self, String> {
|
||||
const DEFAULT_LOGGING_LEVEL: Level = Level::INFO;
|
||||
Ok(LoggingSettings {
|
||||
level: _config
|
||||
.get_string(LOGGING_LEVEL_KEY)
|
||||
.unwrap_or_else(|err| {
|
||||
warn!(
|
||||
"Failed to read {} from configuration, defaulting to {}. Error: {}",
|
||||
LOGGING_LEVEL_KEY, DEFAULT_LOGGING_LEVEL, err
|
||||
);
|
||||
DEFAULT_LOGGING_LEVEL.to_string()
|
||||
})
|
||||
.parse()
|
||||
.unwrap_or_else(|err| {
|
||||
warn!(
|
||||
"Invalid logging level in configuration, defaulting to {}. Error: {}",
|
||||
DEFAULT_LOGGING_LEVEL, err
|
||||
);
|
||||
DEFAULT_LOGGING_LEVEL
|
||||
}),
|
||||
utc: _config
|
||||
.get_bool(LOGGING_UTC_KEY)
|
||||
.unwrap_or_else(|err: ConfigError| {
|
||||
const DEFAULT_UTC: bool = false;
|
||||
warn!(
|
||||
"Invalid UTC setting in configuration, defaulting to {}. Error: {}",
|
||||
DEFAULT_UTC, err
|
||||
);
|
||||
DEFAULT_UTC
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
fn validate(&self) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user