-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextLog
integrations with native loggers
#3522
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5987f6
python: implement native logger integration
teh-cmc 97be8a3
rust: implement native logger integration
teh-cmc e8d3a78
add doc examples for py and rust
teh-cmc 9342f7d
add howto
teh-cmc 9036bc4
lints
teh-cmc 25fd272
doc lint
teh-cmc ac88cd4
make sure to dump tables when a roundtrip fails on CI
teh-cmc 0a6e30e
title that actually fit
teh-cmc 9a76ada
Revert "make sure to dump tables when a roundtrip fails on CI"
teh-cmc 2efc524
rebase filesink PR so we don't need those hacks anymore
teh-cmc ca59a74
xxx
teh-cmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) | ||
teh-cmc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.init()?; | ||
log::info!("This INFO log got added through the standard logging interface"); | ||
|
||
rerun::native_viewer::show(storage.take())?; | ||
Ok(()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary
.into()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TextLogLevel::TRACE
& friends are actually&str
s becauseTextLogLevel
isn't const