#python #logging #pyo3 #log-messages #applications #module #info

pyo3-pylogger

使用 logging 模块为基于 pyo3 的 Rust 应用程序启用 log

4个版本

0.3.0 2024年8月1日
0.2.2 2023年12月24日
0.2.0 2022年12月27日
0.1.1 2022年12月27日

#171调试

Download history 370/week @ 2024-04-26 255/week @ 2024-05-03 163/week @ 2024-05-10 238/week @ 2024-05-17 333/week @ 2024-05-24 374/week @ 2024-05-31 422/week @ 2024-06-07 314/week @ 2024-06-14 363/week @ 2024-06-21 292/week @ 2024-06-28 271/week @ 2024-07-05 338/week @ 2024-07-12 347/week @ 2024-07-19 678/week @ 2024-07-26 547/week @ 2024-08-02 428/week @ 2024-08-09

每月 2,053 次下载

Apache-2.0

9KB
80

pyo3-pylogger

使用Python的 logging 模块为嵌入Python的pyo3应用程序启用日志消息。

使用方法

use log::{info, warn};
use pyo3::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("import logging", None, None).unwrap();
        // The root python logger will be set to "WARNING" by default

        py.run("logging.getLogger().setLevel(0)", None, None).unwrap();

        // Log messages are forwarded to `log` and dealt with by the subscriber
        py.run("logging.debug('DEBUG')", None, None).unwrap();
        py.run("logging.info('INFO')", None, None).unwrap();
        py.run("logging.warning('WARNING')", None, None).unwrap();
        py.run("logging.error('ERROR')", None, None).unwrap();
        py.run("logging.critical('CRITICAL')", None, None).unwrap();
    });
}

输出

[2023-03-06T20:14:15Z INFO  example_project] Just some normal information!
[2023-03-06T20:14:15Z WARN  example_project] Something spooky happened!
[2023-03-06T20:14:15Z DEBUG example_application_py_logger] DEBUG
[2023-03-06T20:14:15Z INFO  example_application_py_logger] INFO
[2023-03-06T20:14:15Z WARN  example_application_py_logger] WARNING
[2023-03-06T20:14:15Z ERROR example_application_py_logger] ERROR
[2023-03-06T20:14:15Z ERROR example_application_py_logger] CRITICAL

依赖项

~2.5MB
~48K SLoC