Macro soroban_sdk::log

source ·
macro_rules! log {
    ($env:expr, $fmt:literal $(,)?) => { ... };
    ($env:expr, $fmt:literal, $($args:expr),* $(,)?) => { ... };
}
Expand description

Log a debug event.

Takes a Env, and a literal format string that containing {} for each additional argument. Arguments may be any value that are convertible to RawVal.

log! statements are only enabled in non optimized builds that have debug-assertions enabled. To enable debug-assertions add the following lines to Cargo.toml, then build with the profile specified, --profile release-with-logs. See the cargo docs for how to use custom profiles.

[profile.release-with-logs]
inherits = "release"
debug-assertions = true

Examples

Log a string:

use soroban_sdk::{log, Env};

let env = Env::default();

log!(&env, "a log entry");

Log a string with values:

use soroban_sdk::{log, symbol, Env};

let env = Env::default();

let value = 5;
log!(&env, "a log entry: {}, {}", value, symbol!("another"));

Assert on logs in tests:

use soroban_sdk::{log, symbol, Env};

let env = Env::default();

let value = 5;
log!(&env, "a log entry: {}, {}, {}", value, symbol!("another"), (1, 2, 1));

use soroban_sdk::testutils::Logger;

assert_eq!(
    env.logger().all(),
    std::vec![
        "a log entry: I32(5), Symbol(another), Object(Vec(#4))".to_string(),
    ],
);