-
Notifications
You must be signed in to change notification settings - Fork 26
Add tracing_debug feature to expose teach-me logging configuration
#386
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
base: main
Are you sure you want to change the base?
Changes from all commits
f082255
95d9ca0
17ea0dd
3a946d1
5b58a5a
File filter
Filter by extension
Conversations
Jump to
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| //! Tracing configuration for debugging and teach-me mode. | ||
| //! | ||
| //! This module provides a default tracing subscriber configuration that enables | ||
| //! all `--teach-me` messages and detailed logging output. | ||
| //! | ||
| //! # Example | ||
| //! | ||
| //! ```no_run | ||
| //! # #[cfg(feature = "tracing_debug")] | ||
| //! cargo_near_build::init_tracing_debug().expect("Failed to initialize tracing"); | ||
| //! ``` | ||
|
|
||
| use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; | ||
|
|
||
| /// Initializes a tracing subscriber with configuration suitable for debugging. | ||
| /// | ||
| /// This function sets up a tracing subscriber that: | ||
| /// - Enables all `near_teach_me` target messages (INFO level) | ||
| /// - Shows WARN level messages from all sources | ||
| /// - Respects RUST_LOG environment variable if set | ||
| /// | ||
| /// This is the same configuration used by `cargo near --teach-me` flag. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// - The tracing subscriber fails to initialize | ||
| /// - The environment filter configuration is invalid | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```no_run | ||
| /// # #[cfg(feature = "tracing_debug")] | ||
| /// cargo_near_build::init_tracing_debug().expect("Failed to initialize tracing"); | ||
| /// ``` | ||
| pub fn init_tracing_debug() -> Result<(), Box<dyn std::error::Error>> { | ||
| use tracing::{Event, Level, Subscriber}; | ||
| use tracing_subscriber::{ | ||
| fmt::{format::Writer, FmtContext, FormatEvent, FormatFields}, | ||
| registry::LookupSpan, | ||
| }; | ||
|
|
||
| // Note: This SimpleFormatter is duplicated from cargo-near/src/lib.rs to avoid | ||
| // introducing additional dependencies or making internal modules public. | ||
| // Future: Consider extracting to a shared module if this needs to be reused elsewhere. | ||
| struct SimpleFormatter; | ||
|
|
||
| impl<S, N> FormatEvent<S, N> for SimpleFormatter | ||
| where | ||
| S: Subscriber + for<'a> LookupSpan<'a>, | ||
| N: for<'a> FormatFields<'a> + 'static, | ||
| { | ||
| fn format_event( | ||
| &self, | ||
| ctx: &FmtContext<'_, S, N>, | ||
| mut writer: Writer<'_>, | ||
| event: &Event<'_>, | ||
| ) -> std::fmt::Result { | ||
| let level = *event.metadata().level(); | ||
| let (icon, color_code) = match level { | ||
| Level::TRACE => ("TRACE ", "\x1b[35m"), // Magenta | ||
| Level::DEBUG => ("DEBUG ", "\x1b[34m"), // Blue | ||
| Level::INFO => ("", ""), // Default | ||
| Level::WARN => ("Warning ", "\x1b[33m"), // Yellow | ||
| Level::ERROR => ("ERROR ", "\x1b[31m"), // Red | ||
| }; | ||
|
|
||
| write!(writer, "{}├ {}", color_code, icon)?; | ||
| write!(writer, "\x1b[0m")?; | ||
|
|
||
| ctx.field_format().format_fields(writer.by_ref(), event)?; | ||
|
|
||
| writeln!(writer) | ||
| } | ||
| } | ||
|
|
||
| let env_filter = EnvFilter::from_default_env() | ||
| .add_directive(Level::WARN.into()) | ||
| .add_directive("near_teach_me=info".parse()?) | ||
| .add_directive("near_cli_rs=info".parse()?) | ||
| .add_directive("tracing_instrument=info".parse()?); | ||
|
Comment on lines
+78
to
+81
|
||
|
|
||
| tracing_subscriber::registry() | ||
| .with(tracing_subscriber::fmt::layer().event_format(SimpleFormatter)) | ||
| .with(env_filter) | ||
| .try_init() | ||
| .map_err(|e| format!("Failed to initialize tracing subscriber: {}", e).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.
The SimpleFormatter implementation is duplicated from cargo-near/src/lib.rs. While the comment at line 43-45 acknowledges this, this creates a maintenance burden where changes to formatting in one location must be manually replicated to the other. Consider extracting SimpleFormatter to a shared internal utility crate or module that both cargo-near and cargo-near-build can depend on, even if it's not part of the public API.