Skip to content

Commit 4c421da

Browse files
author
Dylan Storey
committed
adding update from NfNitLoop
1 parent 2a9e9ed commit 4c421da

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

example_project/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ fn main() {
2525
py.run("logging.warning('WARNING')", None, None).unwrap();
2626
py.run("logging.error('ERROR')", None, None).unwrap();
2727
py.run("logging.critical('CRITICAL')", None, None).unwrap();
28+
29+
30+
py.run("logging.getLogger('foo.bar.baz').info('INFO')", None, None).unwrap();
2831
});
2932
}

src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn register(target: &str) {
1313

1414
/// Consume a Python `logging.LogRecord` and emit a Rust `Log` instead.
1515
#[pyfunction]
16-
fn host_log(record: &PyAny, target: &str) -> PyResult<()> {
16+
fn host_log(record: &PyAny, rust_target: &str) -> PyResult<()> {
1717
let level = record.getattr("levelno")?;
1818
let message = record.getattr("getMessage")?.call0()?.to_string();
1919
let pathname = record.getattr("pathname")?.to_string();
@@ -22,7 +22,19 @@ fn host_log(record: &PyAny, target: &str) -> PyResult<()> {
2222
.to_string()
2323
.parse::<u32>()
2424
.unwrap();
25-
let _logger_name = record.getattr("name")?.to_string();
25+
26+
let logger_name = record.getattr("name")?.to_string();
27+
28+
let full_target: Option<String> = if logger_name.trim().is_empty() || logger_name == "root" {
29+
None
30+
} else {
31+
// Libraries (ex: tracing_subscriber::filter::Directive) expect rust-style targets like foo::bar,
32+
// and may not deal well with "." as a module separator:
33+
let logger_name = logger_name.replace(".", "::");
34+
Some(format!("{rust_target}::{logger_name}"))
35+
};
36+
let target = full_target.as_ref().map(|x| x.as_str()).unwrap_or(rust_target);
37+
2638

2739
// error
2840
let error_metadata = if level.ge(40u8)? {

0 commit comments

Comments
 (0)