Skip to content

Commit 7290042

Browse files
authored
Make tracing::log work in the runtime (#4863)
Co-authored-by: command-bot <>
1 parent 8af45d2 commit 7290042

6 files changed

Lines changed: 27 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prdoc/pr_4863.prdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: "Make `tracing::log` work in the runtime"
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Make `tracing::log` work in the runtime as `log` works in the runtime.
7+
8+
crates:
9+
- name: sp-runtime
10+
bump: patch

substrate/primitives/runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sp-io = { workspace = true }
3434
sp-std = { workspace = true }
3535
sp-weights = { workspace = true }
3636
docify = { workspace = true }
37+
tracing = { workspace = true, features = ["log"], default-features = false }
3738

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

@@ -69,6 +70,7 @@ std = [
6970
"sp-std/std",
7071
"sp-tracing/std",
7172
"sp-weights/std",
73+
"tracing/std",
7274
]
7375

7476
# Serde support without relying on std features.

substrate/primitives/runtime/src/runtime_logger.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,35 @@ impl log::Log for RuntimeLogger {
6666
#[cfg(test)]
6767
mod tests {
6868
use sp_api::ProvideRuntimeApi;
69-
use std::{env, str::FromStr};
69+
use std::env;
7070
use substrate_test_runtime_client::{
7171
runtime::TestAPI, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt,
7272
};
7373

7474
#[test]
75-
fn ensure_runtime_logger_respects_host_max_log_level() {
75+
fn ensure_runtime_logger_works() {
7676
if env::var("RUN_TEST").is_ok() {
7777
sp_tracing::try_init_simple();
78-
log::set_max_level(log::LevelFilter::from_str(&env::var("RUST_LOG").unwrap()).unwrap());
7978

8079
let client = TestClientBuilder::new().build();
8180
let runtime_api = client.runtime_api();
8281
runtime_api
8382
.do_trace_log(client.chain_info().genesis_hash)
8483
.expect("Logging should not fail");
8584
} else {
86-
for (level, should_print) in &[("trace", true), ("info", false)] {
85+
for (level, should_print) in &[("test=trace", true), ("info", false)] {
8786
let executable = std::env::current_exe().unwrap();
8887
let output = std::process::Command::new(executable)
8988
.env("RUN_TEST", "1")
9089
.env("RUST_LOG", level)
91-
.args(&["--nocapture", "ensure_runtime_logger_respects_host_max_log_level"])
90+
.args(&["--nocapture", "ensure_runtime_logger_works"])
9291
.output()
9392
.unwrap();
9493

9594
let output = String::from_utf8(output.stderr).unwrap();
9695
assert!(output.contains("Hey I'm runtime") == *should_print);
96+
assert!(output.contains("THIS IS TRACING") == *should_print);
97+
assert!(output.contains("Hey, I'm tracing") == *should_print);
9798
}
9899
}
99100
}

substrate/test-utils/runtime/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ sp-externalities = { workspace = true }
5353
array-bytes = { optional = true, workspace = true, default-features = true }
5454
serde_json = { workspace = true, features = ["alloc"] }
5555
log = { workspace = true }
56-
hex-literal = { workspace = true, default-features = true }
56+
tracing = { workspace = true, default-features = false }
5757

5858
[dev-dependencies]
5959
futures = { workspace = true }
@@ -112,6 +112,7 @@ std = [
112112
"sp-trie/std",
113113
"sp-version/std",
114114
"substrate-wasm-builder",
115+
"tracing/std",
115116
"trie-db/std",
116117
]
117118

substrate/test-utils/runtime/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,11 @@ impl_runtime_apis! {
596596
}
597597

598598
fn do_trace_log() {
599-
log::trace!("Hey I'm runtime");
599+
log::trace!(target: "test", "Hey I'm runtime");
600+
601+
let data = "THIS IS TRACING";
602+
603+
tracing::trace!(target: "test", %data, "Hey, I'm tracing");
600604
}
601605

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

0 commit comments

Comments
 (0)