From 2cdaf51bf798487273877ae3ac479e70c43f7e3f Mon Sep 17 00:00:00 2001 From: chrysn Date: Sun, 3 Nov 2024 02:20:28 +0000 Subject: [PATCH 1/3] python: Use Python's logging through pyo3_log --- lakers-python/Cargo.toml | 2 +- lakers-python/src/lib.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lakers-python/Cargo.toml b/lakers-python/Cargo.toml index d60b0aa8..dd5d2e85 100644 --- a/lakers-python/Cargo.toml +++ b/lakers-python/Cargo.toml @@ -14,7 +14,7 @@ lakers-ead-authz = { path = "../ead/lakers-ead-authz", features = [ "log" ] } lakers-shared = { path = "../shared", features = ["python-bindings", "quadruple_sizes"] } lakers-crypto = { path = "../crypto", default-features = false, features = ["rustcrypto"] } log = "0.4" -env_logger = "0.9" +pyo3-log = "0.11.0" [dev-dependencies] # We don't need it to build, but it is listed in the manifest Cargo.toml, and diff --git a/lakers-python/src/lib.rs b/lakers-python/src/lib.rs index 89309ef2..d2c4e40f 100644 --- a/lakers-python/src/lib.rs +++ b/lakers-python/src/lib.rs @@ -2,7 +2,6 @@ /// Note that this module is not restricted by no_std. use lakers::*; // use lakers_ead_authz::consts::*; -use env_logger; use lakers_crypto::{default_crypto, CryptoTrait}; use log::trace; use pyo3::wrap_pyfunction; @@ -72,8 +71,10 @@ impl AutoCredential { #[pyo3(name = "lakers")] fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { // initialize the logger once when the module is imported - if env_logger::try_init().is_ok() { - trace!("lakers-python initialized from Rust side."); + if !pyo3_log::try_init().is_ok() { + // Not logging anything in the successful case as per pyo3_log recommendations: That would + // cache the current logging configuration, which likely changes soon after the imports. + log::error!("lakers-python failed to set up (different logger configured?)"); } m.add_function(wrap_pyfunction!(p256_generate_key_pair, m)?)?; From 28b1c75839fe75d0b8c06db4a64a6c5d4c187ace Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 6 Nov 2024 01:26:14 +0000 Subject: [PATCH 2/3] python: Enable trace logging --- lakers-python/src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lakers-python/src/lib.rs b/lakers-python/src/lib.rs index d2c4e40f..5619b2af 100644 --- a/lakers-python/src/lib.rs +++ b/lakers-python/src/lib.rs @@ -69,12 +69,15 @@ impl AutoCredential { // this name must match `lib.name` in `Cargo.toml` #[pymodule] #[pyo3(name = "lakers")] -fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { +fn lakers_python(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { // initialize the logger once when the module is imported - if !pyo3_log::try_init().is_ok() { + if let Err(e) = pyo3_log::Logger::new(py, pyo3_log::Caching::LoggersAndLevels)? + .filter(log::LevelFilter::Trace) + .install() + { // Not logging anything in the successful case as per pyo3_log recommendations: That would // cache the current logging configuration, which likely changes soon after the imports. - log::error!("lakers-python failed to set up (different logger configured?)"); + log::error!("lakers-python failed to set up: {e}"); } m.add_function(wrap_pyfunction!(p256_generate_key_pair, m)?)?; @@ -91,7 +94,7 @@ fn lakers_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; - let submodule = PyModule::new_bound(_py, "consts")?; + let submodule = PyModule::new_bound(py, "consts")?; submodule.add("EAD_AUTHZ_LABEL", lakers_ead_authz::consts::EAD_AUTHZ_LABEL)?; m.add_submodule(&submodule)?; Ok(()) From 96e028603029bf6bacef6d287cb94e61e111bfa4 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 6 Nov 2024 01:35:22 +0000 Subject: [PATCH 3/3] python: Enable trace logging, add documentation --- lakers-python/src/lib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lakers-python/src/lib.rs b/lakers-python/src/lib.rs index 5619b2af..9f520385 100644 --- a/lakers-python/src/lib.rs +++ b/lakers-python/src/lib.rs @@ -66,8 +66,17 @@ impl AutoCredential { } } -// this name must match `lib.name` in `Cargo.toml` +/// Lakers implementation of EDHOC. +/// +/// The `EdhocInitiator` and `EdhocResponder` are entry points to this module. +/// +/// Operations in this module produce logging entries on the `lakers.initiator` and +/// `lakers.responder` logger names. Due to implementation details of `pyo3_log`, Python's log +/// levels are cached in the Rust implementation. It is recommended that the full logging +/// is configured before creating Lakers objects. A setup with `logging.basicConfig(loglevel=5)` +/// will also show Lakers' trace level log messages, which have no equivalent Python level. #[pymodule] +// this name must match `lib.name` in `Cargo.toml` #[pyo3(name = "lakers")] fn lakers_python(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { // initialize the logger once when the module is imported @@ -75,8 +84,7 @@ fn lakers_python(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { .filter(log::LevelFilter::Trace) .install() { - // Not logging anything in the successful case as per pyo3_log recommendations: That would - // cache the current logging configuration, which likely changes soon after the imports. + // Not logging anything in the successful case (see module level docs) log::error!("lakers-python failed to set up: {e}"); }