Skip to content

Commit 7f41ea9

Browse files
feat: Add ONTOENV_LOG support and refactor logging initialization
Co-authored-by: aider (gemini/gemini-2.5-pro) <[email protected]>
1 parent 9963f8e commit 7f41ea9

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

cli/src/main.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,19 @@ fn handle_config_command(config_cmd: ConfigCommands, temporary: bool) -> Result<
352352
}
353353

354354
fn main() -> Result<()> {
355+
ontoenv::api::init_logging();
355356
let cmd = Cli::parse();
356357

357-
let log_level = if cmd.verbose { "info" } else { "warn" };
358-
let log_level = if cmd.debug { "debug" } else { log_level };
359-
std::env::set_var("RUST_LOG", log_level);
358+
// The RUST_LOG env var is set by `init_logging` if ONTOENV_LOG is present.
359+
// CLI flags for verbosity take precedence. If nothing is set, we default to "warn".
360+
if cmd.debug {
361+
std::env::set_var("RUST_LOG", "debug");
362+
} else if cmd.verbose {
363+
std::env::set_var("RUST_LOG", "info");
364+
} else if std::env::var("RUST_LOG").is_err() {
365+
// If no CLI flags and no env var is set, default to "warn".
366+
std::env::set_var("RUST_LOG", "warn");
367+
}
360368
env_logger::init();
361369

362370
let policy = cmd.policy.unwrap_or_else(|| "default".to_string());

lib/src/api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ use petgraph::graph::{Graph as DiGraph, NodeIndex};
2323
use std::collections::{HashMap, HashSet, VecDeque};
2424
use std::fs;
2525

26+
/// Initializes logging for the ontoenv library.
27+
///
28+
/// This function checks for the `ONTOENV_LOG` environment variable. If it is set,
29+
/// `RUST_LOG` is set to its value. `ONTOENV_LOG` takes precedence over `RUST_LOG`.
30+
/// The logger initialization (e.g., `env_logger::init()`) must be called after
31+
/// this function for the log level to take effect.
32+
pub fn init_logging() {
33+
if let Ok(log_level) = std::env::var("ONTOENV_LOG") {
34+
std::env::set_var("RUST_LOG", log_level);
35+
}
36+
}
37+
2638
/// Searches for the .ontoenv directory in the given directory and then recursively up its parent directories.
2739
/// Returns the path to the directory containing the .ontoenv directory if found.
2840
pub fn find_ontoenv_root_from(start_dir: &Path) -> Option<PathBuf> {

python/src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ use pyo3::{
1313
use std::borrow::Borrow;
1414
use std::collections::{HashMap, HashSet};
1515
use std::path::PathBuf;
16-
use std::sync::{Arc, Mutex, Once};
16+
use std::sync::{Arc, Mutex};
1717

1818
fn anyhow_to_pyerr(e: Error) -> PyErr {
1919
PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string())
2020
}
2121

22-
static INIT: Once = Once::new();
23-
2422
#[allow(dead_code)]
2523
struct MyTerm(Term);
2624
impl From<Result<Bound<'_, PyAny>, pyo3::PyErr>> for MyTerm {
@@ -248,12 +246,6 @@ impl OntoEnv {
248246
recreate: bool,
249247
read_only: bool,
250248
) -> PyResult<Self> {
251-
// wrap env_logger::init() in a Once to ensure it's only called once. This can
252-
// happen if a user script creates multiple OntoEnv instances
253-
INIT.call_once(|| {
254-
env_logger::init();
255-
});
256-
257249
let env = if let Some(c) = config {
258250
let config_path = path.unwrap_or_else(|| PathBuf::from("."));
259251
// if temporary is true, create a new OntoEnv
@@ -952,6 +944,11 @@ impl OntoEnv {
952944

953945
#[pymodule]
954946
fn ontoenv(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
947+
// Initialize logging when the python module is loaded.
948+
::ontoenv::api::init_logging();
949+
// Use try_init to avoid panic if logging is already initialized.
950+
let _ = env_logger::try_init();
951+
955952
m.add_class::<Config>()?;
956953
m.add_class::<OntoEnv>()?;
957954
m.add_class::<PyOntology>()?;

0 commit comments

Comments
 (0)