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 all 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
3 changes: 2 additions & 1 deletion Cargo.lock

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

10 changes: 10 additions & 0 deletions prdoc/pr_4863.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: "Make `tracing::log` work in the runtime"

doc:
- audience: Runtime Dev
description: |
Make `tracing::log` work in the runtime as `log` works in the runtime.

crates:
- name: sp-runtime
bump: patch
2 changes: 2 additions & 0 deletions substrate/primitives/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-io = { workspace = true }
sp-std = { workspace = true }
sp-weights = { workspace = true }
docify = { workspace = true }
tracing = { workspace = true, features = ["log"], default-features = false }

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

Expand Down Expand Up @@ -69,6 +70,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
3 changes: 2 additions & 1 deletion substrate/test-utils/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sp-externalities = { workspace = true }
array-bytes = { optional = true, workspace = true, default-features = true }
serde_json = { workspace = true, features = ["alloc"] }
log = { workspace = true }
hex-literal = { workspace = true, default-features = true }
tracing = { workspace = true, default-features = false }

[dev-dependencies]
futures = { workspace = true }
Expand Down Expand Up @@ -112,6 +112,7 @@ std = [
"sp-trie/std",
"sp-version/std",
"substrate-wasm-builder",
"tracing/std",
"trie-db/std",
]

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