#python #pyo3 #logging #module #warn

pyo3-pylogger

Enables log for pyo3 based Rust applications using the logging modules

5 unstable releases

new 0.4.0 Mar 31, 2025
0.3.0 Aug 1, 2024
0.2.2 Dec 24, 2023
0.2.0 Dec 27, 2022
0.1.1 Dec 27, 2022

#580 in Debugging

Download history 361/week @ 2024-12-08 332/week @ 2024-12-15 109/week @ 2024-12-22 276/week @ 2024-12-29 302/week @ 2025-01-05 354/week @ 2025-01-12 289/week @ 2025-01-19 319/week @ 2025-01-26 768/week @ 2025-02-02 542/week @ 2025-02-09 360/week @ 2025-02-16 515/week @ 2025-02-23 902/week @ 2025-03-02 471/week @ 2025-03-09 400/week @ 2025-03-16 810/week @ 2025-03-23

2,609 downloads per month

Apache-2.0

14KB
159 lines

pyo3-pylogger

Enables log messages for pyo3 embedded Python applications using Python's logging module.

Features

  • Logging integration between Python's logging module and Rust's log crate
  • Structured logging support via the logging extra field (requires kv feature)

Usage

use log::{info, warn};
use pyo3::{ffi::c_str, prelude::*};
fn main() {
    // register the host handler with python logger, providing a logger target
    pyo3_pylogger::register("example_application_py_logger");

    // initialize up a logger
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
    //just show the logger working from Rust.
    info!("Just some normal information!");
    warn!("Something spooky happened!");

    // Ask pyo3 to set up embedded Python interpreter
    pyo3::prepare_freethreaded_python();
    Python::with_gil(|py| {
        // Python code can now `import logging` as usual
        py.run(
            c_str!(
                r#"
import logging
logging.getLogger().setLevel(0)
logging.debug('DEBUG')
logging.info('INFO')
logging.warning('WARNING')
logging.error('ERROR')
logging.getLogger('foo.bar.baz').info('INFO')"#
            ),
            None,
            None,
        )
        .unwrap();
    })
}


Outputs

[2025-03-28T01:12:29Z INFO  helloworld] Just some normal information!
[2025-03-28T01:12:29Z WARN  helloworld] Something spooky happened!
[2025-03-28T01:12:29Z DEBUG example_application_py_logger] DEBUG
[2025-03-28T01:12:29Z INFO  example_application_py_logger] INFO
[2025-03-28T01:12:29Z WARN  example_application_py_logger] WARNING
[2025-03-28T01:12:29Z ERROR example_application_py_logger] ERROR
[2025-03-28T01:12:29Z INFO  example_application_py_logger::foo::bar::baz] INFO

Structured Logging

To enable structured logging support, add the kv feature to your Cargo.toml:

[dependencies]
pyo3-pylogger = { version = "0.3", features = ["kv"] }

Then you can use Python's extra parameter to pass structured data:

logging.info("Processing order", extra={"order_id": "12345", "amount": 99.99})

When using a structured logging subscriber in Rust, these key-value pairs will be properly captured, for example:

[2025-03-28T01:12:29Z INFO  example_application_py_logger] Processing order order_id=12345 amount=99.99

Feature Flags

  • kv: Enables structured logging support via Python's extra fields. This adds support for the log crate's key-value system.

Dependencies

~2.5MB
~56K SLoC