When working on Parity Substrate runtime development, how can I print out debug message for tracing and inspecting my variables?
-
1You tagged your question with `substrate`, would you be interested in a dedicated Stack Exchange Q&A site for Substrate, Polkadot, et al. -- check out the [Area51 Substrate Proposal](https://area51.stackexchange.com/proposals/122626/substrate?referrer=NTUwMTkxYjJjOTJiNjE0YzMxYjgwMGNkZmFlYzdhZTczYjk1ZWY3ZGI4NzJmODUwN2RlYTQ2MTNjZTdkOTZhMAzuL-zybtPN9CHzwE-WUdvBC8WxvPG46b4ayadke6kG0) – q9f Jul 28 '19 at 10:59
4 Answers
Both of the above answers are correct in their own sense/time. Here's a more accurate overview:
runtime_io::print("...");
has been moved. You can now use the same function fromsp-runtime::print()
. These will be visible in a log target namedruntime
and levelDEBUG
. So you'd have to doRUST_LOG=runtime=debug
. You are still calling into [sp_io
under the hood though]. Similar functions are provided unframe_support::print
andframe_support::debug
as well.- If you want to have more control over the log target/level, you can either directly use the
log
crate. Similarly, you need to make sure that you are enabling the appropriate log target viaRUST_LOG
. Be ware that you if you forget to specify the log target inlog
, the crate path will be used by default. - If you want to compile for wasm and native, and want prints only for native execution, use
sp_std::if_std!{}
macro.
A final useful tip is to: when possible, you can just bloat your code with println!
and do SKIP_WASM_BUILD=1 cargo run [xxx]
. This is helpful when you are developing and want quick debug prints without any of the setup explained above.

- 1,365
- 12
- 20
-
1if you want to print logs to your terminal output to debug the value of variables when running tests for a specific pallet, you can run add the following in a function of your pallet `println!("
{:#?}", – Luke Schoen Oct 06 '21 at 05:17);`, and then run the tests with `SKIP_WASM_BUILD=1 RUST_LOG=runtime=debug cargo test --package -- --nocapture` -
@LukeSchoen your answer should be the best, up-to-date answer! Thank you – Russo Mar 21 '22 at 06:54
You can also use the if_std!
macro included with sp-std
:
https://github.com/paritytech/substrate/pull/2979
if_std!
is a feature gate that should only be run when std
feature is enabled.
Example
sp_std::if_std! {
// This code is only being compiled and executed when the `std` feature is enabled.
println!("Hello native world");
}
This is better because you can println
variables and stuff rather than simply printing a string.

- 12,206
- 1
- 38
- 69
As a newcomer to Substrate development, the most direct way I found is with runtime_io::print()
.
Example:
use runtime_io::{ self };
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
pub fn my_func(origin) -> Result {
runtime_io::print("Hello World");
Ok(());
}
}
}
The message will then appear in the console. Pay quick attention to it as it is constantly scrolling.
For a complete example, refer to the TCR tutorial example in github.

- 972
- 8
- 27
-
This answer is no longer valid and goes back to prior to crates renames in substrate. – kianenigma Feb 21 '20 at 08:36