From bd65c0ada22e4bd371022c2789350658ac789b78 Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:01:34 +0300 Subject: [PATCH] [logging]: send Rust log messages to Python --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 2 ++ src/lib.rs | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index abdf3f1..3dc4786 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,12 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + [[package]] name = "async-compression" version = "0.4.12" @@ -899,7 +905,9 @@ dependencies = [ "encoding_rs", "html2text", "indexmap", + "log", "pyo3", + "pyo3-log", "rquest", "tokio", ] @@ -969,6 +977,17 @@ dependencies = [ "pyo3-build-config", ] +[[package]] +name = "pyo3-log" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ac84e6eec1159bc2a575c9ae6723baa6ee9d45873e9bebad1e3ad7e8d28a443" +dependencies = [ + "arc-swap", + "log", + "pyo3", +] + [[package]] name = "pyo3-macros" version = "0.22.2" diff --git a/Cargo.toml b/Cargo.toml index 0c3f87b..b86be6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,8 @@ crate-type = ["cdylib"] [dependencies] pyo3 = { version = "0.22", features = ["extension-module", "abi3-py38", "indexmap", "anyhow"] } anyhow = "1" +log = "0.4" +pyo3-log = "0.11" rquest = { version = "0.20", default-features = false, features = [ "boring-tls", "http2", diff --git a/src/lib.rs b/src/lib.rs index ff24d9e..0ed191a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -386,6 +386,8 @@ impl Client { let url = PyString::new_bound(py, &f_url).unbind(); let content = PyBytes::new_bound(py, &f_buf).unbind(); + log::info!("response: {} {} {} {}", f_url, f_status_code, f_buf.len(), f_encoding); + Ok(Response { content, cookies, @@ -1004,6 +1006,8 @@ fn patch( #[pymodule] fn primp(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + pyo3_log::init(); + m.add_class::()?; m.add_function(wrap_pyfunction!(request, m)?)?; m.add_function(wrap_pyfunction!(get, m)?)?;