pyo3
pyo3 copied to clipboard
Python to Rust logging implementation
As part of our project, we embed Python code in Rust workers using PyO3 (see this project). Although the binding is really working well, we lack the Python to Rust logging implementation as it would be much more easier to track potential crashes of the worker.
We would really be glad to bring this feature to the project and we would appreciate to have some guidance from the PyO3 community ! :)
Hi @nipierre, glad you're having a positive experience with PyO3 and many thanks for your interest in contributing to the ecosystem!
I think the correct design for a Python -> Rust logging bridge is to create a subclass of logging.Handler which calls into the Rust log ecosystem.
I'm not 100% certain whether it should be a Rust crate which can included via cargo, or a Python package which can be bundled separarately.
Similar to pyo3-log, which does the Rust -> Python direction, I'd be happy to include information about this new library in the PyO3 README and guide.
Also please feel free to ask any further questions and request my review on any code. I think this is an important piece of the ecosystem that I'm happy to help advise on!
I implemented the basic idea @davidhewitt proposed for a tracing based project (interop with log should just work (tm), although it is basically untested). While my implementation is quite quick and dirty, it should tick the major boxes and delivers logging messages to the host.
In fact, it works for me doing the full roundabout:
- extension.so calls
tracing pyo3-logdelivers the message to pythonslogging- the handler delivers the message to the host process
- the host process delivers the message to its subscriber (logger)
- And finally,
tracing-subscriberdisplays the message.