add file options to Logger::init

This commit is contained in:
Christoph J. Scherr 2023-08-01 16:26:31 +02:00
parent 31bd3fb08c
commit cecb07928e
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
2 changed files with 27 additions and 10 deletions

View File

@ -30,6 +30,10 @@ use pyo3::prelude::*;
//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// //// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// The log level used when none is specified /// The log level used when none is specified
pub const DEFAULT_LOG_LEVEL: Level = Level::INFO; pub const DEFAULT_LOG_LEVEL: Level = Level::INFO;
/// The path where logs are stored when no path is given.
///
/// Currently, this is `/dev/null`, meaning they will be written to the void = discarded.
pub const DEFAULT_LOG_DIR: &'static str = "/dev/null";
//// STATICS /////////////////////////////////////////////////////////////////////////////////////// //// STATICS ///////////////////////////////////////////////////////////////////////////////////////
static INITIALIZED: AtomicBool = AtomicBool::new(false); static INITIALIZED: AtomicBool = AtomicBool::new(false);
@ -68,15 +72,15 @@ impl Logger {
/// Will enable the logger to be used. /// Will enable the logger to be used.
/// ///
/// Assumes some defaults, use [`init_customized`](init_customized) for more control /// Assumes some defaults, use [`init_customized`](init_customized) for more control
pub fn init() -> Result<()> { pub fn init(log_dir: Option<PathBuf>, max_level: Option<Level>) -> Result<()> {
Self::init_customized( Self::init_customized(
false, log_dir.is_some(),
PathBuf::from("/dev/null"), log_dir.unwrap_or(PathBuf::from(DEFAULT_LOG_DIR)),
true, true,
false, false,
true, true,
false, false,
Level::INFO, max_level.unwrap_or(DEFAULT_LOG_LEVEL),
false, false,
false, false,
false, false,
@ -180,8 +184,21 @@ impl Logger {
/// ## Python version of [`init()`](Logger::init) /// ## Python version of [`init()`](Logger::init)
#[pyo3(name = "init")] #[pyo3(name = "init")]
#[staticmethod] #[staticmethod]
pub fn py_init() -> Result<()> { pub fn py_init(log_dir: Option<PathBuf>, max_level: Option<String>) -> Result<()> {
Self::init() Self::init(
log_dir,
match max_level {
Some(s) => match s.to_uppercase().as_str() {
"TRACE" => Some(tracing::Level::TRACE),
"DEBUG" => Some(tracing::Level::DEBUG),
"INFO" => Some(tracing::Level::INFO),
"WARN" => Some(tracing::Level::WARN),
"ERROR" => Some(tracing::Level::ERROR),
_ => return Err(Error::Usage(format!("'{s}' is not a valid log level"))),
},
None => None,
},
)
} }
/// ## Python version of [`error()`](Logger::error) /// ## Python version of [`error()`](Logger::error)
#[pyo3(name = "error")] #[pyo3(name = "error")]

View File

@ -80,9 +80,9 @@ fn test_multi_initialize() {
setup(); setup();
let l = Logger::new(); let l = Logger::new();
// these should be ignored due to the global flag // these should be ignored due to the global flag
Logger::init().unwrap_err(); Logger::init(None, None).unwrap_err();
Logger::init().unwrap_err(); Logger::init(None, None).unwrap_err();
Logger::init().unwrap_err(); Logger::init(None, None).unwrap_err();
Logger::init().unwrap_err(); Logger::init(None, None).unwrap_err();
l.info("Successfully ignored extra init"); l.info("Successfully ignored extra init");
} }