-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TextLog
integrations with native loggers (#3522)
Everything to seamlessly integrate `TextLog` with native loggers in Python & Rust. - [How-to guide](https://www.rerun.io/preview/154a8a0873def8fbdadc50805778aff2458f08b7/docs/howto/integration-with-native-logger) - Requires #3525 - Fixes #3450
- Loading branch information
Showing
22 changed files
with
358 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use log::Log; | ||
use re_types::{archetypes::TextLog, components::TextLogLevel}; | ||
|
||
use crate::RecordingStream; | ||
|
||
// --- | ||
|
||
/// Implements a [`log::Log`] that forwards all events to the Rerun SDK. | ||
#[derive(Debug)] | ||
pub struct Logger { | ||
rec: RecordingStream, | ||
filter: Option<env_logger::filter::Filter>, | ||
path_prefix: Option<String>, | ||
} | ||
|
||
impl Drop for Logger { | ||
fn drop(&mut self) { | ||
self.flush(); | ||
} | ||
} | ||
|
||
impl Logger { | ||
/// Returns a new [`Logger`] that forwards all events to the specified [`RecordingStream`]. | ||
pub fn new(rec: RecordingStream) -> Self { | ||
Self { | ||
rec, | ||
filter: None, | ||
path_prefix: None, | ||
} | ||
} | ||
|
||
/// Configures the [`Logger`] to prefix the specified `path_prefix` to all events. | ||
pub fn with_path_prefix(mut self, path_prefix: impl Into<String>) -> Self { | ||
self.path_prefix = Some(path_prefix.into()); | ||
self | ||
} | ||
|
||
/// Configures the [`Logger`] to filter events. | ||
/// | ||
/// This uses the familiar [env_logger syntax]. | ||
/// | ||
/// If you don't call this, the [`Logger`] will parse the `RUST_LOG` environment variable | ||
/// instead when you [`Logger::init`] it. | ||
/// | ||
/// [env_logger syntax]: https://docs.rs/env_logger/latest/env_logger/index.html#enabling-logging | ||
pub fn with_filter(mut self, filter: impl AsRef<str>) -> Self { | ||
use env_logger::filter::Builder; | ||
self.filter = Some(Builder::new().parse(filter.as_ref()).build()); | ||
self | ||
} | ||
|
||
/// Sets the [`Logger`] as global logger. | ||
/// | ||
/// All calls to [`log`] macros will go through this [`Logger`] from this point on. | ||
pub fn init(mut self) -> Result<(), log::SetLoggerError> { | ||
if self.filter.is_none() { | ||
use env_logger::filter::Builder; | ||
self.filter = Some(Builder::new().parse(&re_log::default_log_filter()).build()); | ||
} | ||
|
||
// NOTE: We will have to make filtering decisions on a per-crate/module basis, therefore | ||
// there is no global filtering ceiling. | ||
log::set_max_level(log::LevelFilter::max()); | ||
log::set_boxed_logger(Box::new(self)) | ||
} | ||
} | ||
|
||
impl log::Log for Logger { | ||
#[inline] | ||
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool { | ||
self.filter | ||
.as_ref() | ||
.map_or(true, |filter| filter.enabled(metadata)) | ||
} | ||
|
||
#[inline] | ||
fn log(&self, record: &log::Record<'_>) { | ||
if !self | ||
.filter | ||
.as_ref() | ||
.map_or(true, |filter| filter.matches(record)) | ||
{ | ||
return; | ||
} | ||
|
||
let target = record.metadata().target().replace("::", "/"); | ||
let ent_path = if let Some(path_prefix) = self.path_prefix.as_ref() { | ||
format!("{path_prefix}/{target}") | ||
} else { | ||
target | ||
}; | ||
|
||
let level = log_level_to_rerun_level(record.metadata().level()); | ||
|
||
let body = format!("{}", record.args()); | ||
|
||
self.rec | ||
.log(ent_path, &TextLog::new(body).with_level(level)) | ||
.ok(); // ignore error | ||
} | ||
|
||
#[inline] | ||
fn flush(&self) { | ||
self.rec.flush_blocking(); | ||
} | ||
} | ||
|
||
// --- | ||
|
||
fn log_level_to_rerun_level(lvl: log::Level) -> TextLogLevel { | ||
match lvl { | ||
log::Level::Error => TextLogLevel::ERROR, | ||
log::Level::Warn => TextLogLevel::WARN, | ||
log::Level::Info => TextLogLevel::INFO, | ||
log::Level::Debug => TextLogLevel::DEBUG, | ||
log::Level::Trace => TextLogLevel::TRACE, | ||
} | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
"""Log some text entries.""" | ||
"""Shows integration of Rerun's `TextLog` with the native logging interface.""" | ||
import logging | ||
|
||
import rerun as rr | ||
|
||
rr.init("rerun_example_text_entry", spawn=True) | ||
rr.init("rerun_example_text_log_integration", spawn=True) | ||
|
||
# Log a direct entry directly | ||
rr.log_text_entry("logs", "this entry has loglevel TRACE", level="TRACE") | ||
# Log a text entry directly | ||
rr.log("logs", rr.TextLog("this entry has loglevel TRACE", level=rr.TextLogLevel.TRACE)) | ||
|
||
# Or log via a logging handler | ||
logging.getLogger().addHandler(rr.LoggingHandler("logs/handler")) | ||
logging.getLogger().setLevel(-1) | ||
logging.info("This log got added through a `LoggingHandler`") | ||
logging.info("This INFO log got added through the standard logging interface") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Shows integration of Rerun's `TextLog` with the native logging interface. | ||
use rerun::{archetypes::TextLog, components::TextLogLevel, external::log, RecordingStreamBuilder}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let (rec, storage) = | ||
RecordingStreamBuilder::new("rerun_example_text_log_integration").memory()?; | ||
|
||
// Log a text entry directly: | ||
rec.log( | ||
"logs", | ||
&TextLog::new("this entry has loglevel TRACE").with_level(TextLogLevel::TRACE), | ||
)?; | ||
|
||
// Or log via a logging handler: | ||
rerun::Logger::new(rec.clone()) // recording streams are ref-counted | ||
.with_path_prefix("logs/handler") | ||
// You can also use the standard `RUST_LOG` environment variable! | ||
.with_filter(rerun::default_log_filter()) | ||
.init()?; | ||
log::info!("This INFO log got added through the standard logging interface"); | ||
|
||
rerun::native_viewer::show(storage.take())?; | ||
Ok(()) | ||
} |
Oops, something went wrong.