Robert Zaremba
Robert Zaremba

Reputation: 8481

How to capture NEAR samart contract logs (env::log)?

I would like to capture logs (env::log) in NEAR Rust smart contract tests runtime (not a terminal) and inspect the output (in a test function). How can I do it?

Upvotes: 1

Views: 298

Answers (1)

Evgeny Kuzyakov
Evgeny Kuzyakov

Reputation: 1078

This feature is not shipped yet. Here is the PR that will introduce it to the near_sdk https://github.com/near/near-sdk-rs/pull/229. Maybe you can just grab the code that does it, assuming it provides enough visibility.

/// Returns a copy of logs from VMLogic. Only available in unit tests.
pub fn get_logs() -> Vec<String> {
    let blockchain_interface =
        env::take_blockchain_interface().expect("Blockchain interface is not set");
    let logs = blockchain_interface
        .as_mocked_blockchain()
        .expect("MockedBlockchain interface expected")
        .logs();
    env::set_blockchain_interface(blockchain_interface);
    logs
}

Upvotes: 2

Related Questions