Skip to content
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

Make tracing::log work in the runtime #4863

Merged
merged 8 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions substrate/primitives/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ sp-io = { path = "../io", default-features = false }
sp-std = { path = "../std", default-features = false }
sp-weights = { path = "../weights", default-features = false }
docify = "0.2.8"
# Enable the `log` feature so that we see `tracing` logs from the runtime
tracing = { version = "0.1.37", features = ["log"], default-features = false }

simple-mermaid = { version = "0.1.1", optional = true }

Expand Down Expand Up @@ -69,6 +71,7 @@ std = [
"sp-std/std",
"sp-tracing/std",
"sp-weights/std",
"tracing/std",
]

# Serde support without relying on std features.
Expand Down
11 changes: 6 additions & 5 deletions substrate/primitives/runtime/src/runtime_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,35 @@ impl log::Log for RuntimeLogger {
#[cfg(test)]
mod tests {
use sp_api::ProvideRuntimeApi;
use std::{env, str::FromStr};
use std::env;
use substrate_test_runtime_client::{
runtime::TestAPI, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt,
};

#[test]
fn ensure_runtime_logger_respects_host_max_log_level() {
fn ensure_runtime_logger_works() {
if env::var("RUN_TEST").is_ok() {
sp_tracing::try_init_simple();
log::set_max_level(log::LevelFilter::from_str(&env::var("RUST_LOG").unwrap()).unwrap());

let client = TestClientBuilder::new().build();
let runtime_api = client.runtime_api();
runtime_api
.do_trace_log(client.chain_info().genesis_hash)
.expect("Logging should not fail");
} else {
for (level, should_print) in &[("trace", true), ("info", false)] {
for (level, should_print) in &[("test=trace", true), ("info", false)] {
let executable = std::env::current_exe().unwrap();
let output = std::process::Command::new(executable)
.env("RUN_TEST", "1")
.env("RUST_LOG", level)
.args(&["--nocapture", "ensure_runtime_logger_respects_host_max_log_level"])
.args(&["--nocapture", "ensure_runtime_logger_works"])
.output()
.unwrap();

let output = String::from_utf8(output.stderr).unwrap();
assert!(output.contains("Hey I'm runtime") == *should_print);
assert!(output.contains("THIS IS TRACING") == *should_print);
assert!(output.contains("Hey, I'm tracing") == *should_print);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions substrate/test-utils/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ array-bytes = { version = "6.2.2", optional = true }
serde_json = { workspace = true, features = ["alloc"] }
log = { workspace = true }
hex-literal = { version = "0.4.1" }
tracing = { version = "0.1.37", default-features = false }

[dev-dependencies]
futures = "0.3.30"
Expand Down Expand Up @@ -113,6 +114,7 @@ std = [
"sp-version/std",
"substrate-wasm-builder",
"trie-db/std",
"tracing/std",
]

# Special feature to disable logging
Expand Down
6 changes: 5 additions & 1 deletion substrate/test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,11 @@ impl_runtime_apis! {
}

fn do_trace_log() {
log::trace!("Hey I'm runtime");
log::trace!(target: "test", "Hey I'm runtime");

let data = "THIS IS TRACING";

tracing::trace!(target: "test", %data, "Hey, I'm tracing");
}

fn verify_ed25519(sig: ed25519::Signature, public: ed25519::Public, message: Vec<u8>) -> bool {
Expand Down
Loading