stellar-cli. Logs are only compiled into the contract if the debug-assertions Rust compiler option is enabled.
Run the Example
First go through the Setup process to get your development environment configured, then clone thev23.0.0 tag of soroban-examples repository:
logging directory, and use cargo test.
Code
logging/Cargo.toml
logging/src/lib.rs
How it Works
Thelog! macro logs a string. Any logs that occur during execution are outputted to stdout in stellar-cli and available for tests to assert on or print.
Logs are only outputted if the contract is built with the debug-assertions compiler option enabled. This makes them efficient to leave in code permanently since a regular release build will omit them.
Logs are only recorded in Soroban environments that have logging enabled. The only Soroban environments where logging is enabled is in Rust tests, and in the stellar-cli.
Open the files above to follow along.
Cargo.toml Profile
Logs are only outputted if the contract is built with the debug-assertions compiler option enabled.
The test profile that is activated when running cargo test has debug-assertions enabled, so when running tests logs are enabled by default.
A new release-with-logs profile is added to Cargo.toml that inherits from the release profile, and enables debug-assertions. It can be used to build a .wasm file that has logs enabled.
--release or --profile release option.
To build with logs use the --profile release-with-logs option.
Using the log! Macro
The log! macro builds a string from the format string, and a list of arguments. Arguments are substituted wherever the {} value appears in the format string.
value is a Symbol containing "Dev".
Tests
Open thelogging/src/test.rs file to follow along.
logging/src/test.rs
std crate, which contains the Rust standard library, is imported so that the test can use the std::vec! and std::println! macros. Since contracts are required to use #![no_std], tests in contracts must manually import std to use std functionality like printing to stdout.
Env, which is the Soroban environment that the contract will run in.
env.register_at function. This makes it easier to ensure the logging output is coming from the relevant contract address.
impl block that is annotated with the #[contractimpl] attribute have a corresponding function generated in a generated client type. The client type will be named the same as the contract type with Client appended. For example, in our contract the contract type is HelloContract, and the client is named HelloContractClient.
Build the Contract
To build the contract, use thestellar contract build command.
Without Logs
To build the contract without logs, use the--release option.
.wasm file should be outputted in the target directory, in the release subdirectory:
With Logs
To build the contract with logs, use the--profile release-with-logs option.
.wasm file should be outputted in the target directory, in the release-with-logs subdirectory:
Run the Contract
If you havestellar-cli installed, you can deploy the contract and invoke its functions. Specify the -v option to enable verbose logs.
Deploy
- macOS/Linux
- Windows (PowerShell)
Invoke
- macOS/Linux
- Windows (PowerShell)