How to see logging when running integration tests in Rust (using NEAR Workspaces-rs in particular)
Asked Answered
R

2

2

I'm using https://github.com/near/workspaces-rs/ and have lines in my functions like log!("Removed {} from {}", &key, &recipient);

(using use near_sdk::{env, log};)

But those log messages don't appear in the terminal when I run my integration tests.

How to initialize the logger for integration tests? pointed me to https://docs.rs/env_logger/0.9.0/env_logger/index.html#capturing-logs-in-tests which gives this example:

#[cfg(test)]
mod tests {
    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    fn it_works() {
        init();

        info!("This record will be captured by `cargo test`");

        assert_eq!(2, 1 + 1);
    }
}

But even when I create and call that init() function and also replace my log!() calls with info!(), I get nothing.

(I can see the log outputs of the tests themselves but not the logging from within the main implementation code.)

Rotifer answered 9/8, 2022 at 14:45 Comment(0)
I
4

Logs will be shown in the outcome from .call() and can be retrieved from the receipts element in the returned data structure (receipts -> ExecutionOutcome -> logs).

To prove this works, I created a new project in my projects directory with npx create-near-app which comes with a basic get-set string contract in TypeScript. I deleted the integration-tests folder since the implementation was in JS/TS and created a new tests folder in instead, with a similar structure to that seen here: https://github.com/near-examples/NFT/tree/master/integration-tests/rs

Here are the files that I modified: https://gist.github.com/idea404/5ecbcfaa2b1e41b9e33df15dfdaa0dae

Upon running the tests with npm test from the project's root directory, I get the following output:

cargo run --example test
   Compiling ryan-ex v0.1.0 (/Users/dennis/Code/ryan-ex/tests)
    Finished dev [unoptimized + debuginfo] target(s) in 1.24s
     Running `target/debug/examples/test`
starting test
set_greeting outcome: ExecutionFinalResult {
    total_gas_burnt: 10898323344200,
    transaction: ExecutionOutcome {
        block_hash: `7gFBcM8paTWx9FnDVhZvLdYwHAS5YEyGpQYp9Ve8cEq9`,
        logs: [],
        receipt_ids: [
            `4E4cw7uXM31xpifF1tiDgrfaBV8yTuvDa86ucZHDey2o`,
        ],
        gas_burnt: 2428003729558,
        tokens_burnt: 242800372955800000000,
        executor_id: AccountId(
            "dev-20221025122820-11314570895307",
        ),
        status: SuccessReceiptId(4E4cw7uXM31xpifF1tiDgrfaBV8yTuvDa86ucZHDey2o),
    },
    receipts: [
        ExecutionOutcome {
            block_hash: `7gFBcM8paTWx9FnDVhZvLdYwHAS5YEyGpQYp9Ve8cEq9`,
            logs: [
                "Saving greeting hello there",
            ],
            receipt_ids: [
                `ExWePuAjosnbJRQSXzWczfWmbqcu9RwRzQqTsjnssVdo`,
            ],
            gas_burnt: 8247137052142,
            tokens_burnt: 824713705214200000000,
            executor_id: AccountId(
                "dev-20221025122820-11314570895307",
            ),
            status: SuccessValue(``),
        },
        ExecutionOutcome {
            block_hash: `Ed1wmpFgMQEtFAs5RgBzVBmwvyc3Tow2JBHh2mmD9HiC`,
            logs: [],
            receipt_ids: [],
            gas_burnt: 223182562500,
            tokens_burnt: 0,
            executor_id: AccountId(
                "dev-20221025122820-11314570895307",
            ),
            status: SuccessValue(``),
        },
    ],
    status: SuccessValue(``),
}
--------------
"hello there"
Dev Account ID: dev-20221025122820-11314570895307
Insure answered 25/10, 2022 at 12:44 Comment(0)
A
0

Using env::log_str in your smart contract, you can access the logs from the ResultDetails

let outcome = contract
    .call(&worker, "new_default_meta")
    .args_json(json!({
        "owner_id": contract.id(),
    }))?
    .transact()
    .await?;
outcome.logs()
Agglutinin answered 9/8, 2022 at 14:59 Comment(3)
Thanks for your answer. I've been trying to understand it. I'll give a specific example. How I can see the result of this log? github.com/ryancwalsh/donation_matcher_contract/blob/… I just want to see the line printed in the terminal alongside the log lines printed from test log calls like github.com/ryancwalsh/donation_matcher_contract/blob/… Thanks!Rotifer
Use your _donate_result variable or _matcher1_rescind_result. They contain a method/property with execution logs. For instance: println!("{:?}",_donate_result.logs());Agglutinin
Thanks for your response. I just tested this, but unfortunately it only prints whatever the contract function returned rather than what it may have logged along the way via log!().Rotifer

© 2022 - 2024 — McMap. All rights reserved.