Enable debug logs
Sometimes it is easier to troubleshoot applications if all the client library requests and responses are logged to the console. This guide shows you how to enable the logging facilities in the client library.
Prerequisites
This guide uses the Secret Manager API. The same concepts apply to the client libraries for other services. You may want to follow the service quickstart, which shows you how to enable the service.
For complete setup instructions for the Rust libraries, see Setting up your development environment.
Dependencies
You must declare the dependencies in your Cargo.toml file:
cargo add google-cloud-secretmanager-v1 google-cloud-gax
Enable logging
The Rust client libraries use Tokio's tracing crate to collect scoped,
structured, and async-aware diagnostics. The tracing separate sources of
diagnostics (such as the Rust client libraries) from the components that collect
these diagnostics using the Subscriber trait. There are many implementations
available for the Subscriber. In this example we will use the fmt subscriber
included with the tracing-subscriber crate.
First, add a dependency on the tracing-subscriber crate:
cargo add tracing tracing-subscriber
This example receives the project ID as a function parameter:
/// # Parameters
/// - `project_id`: the id of a Google Cloud project, or its numeric ID.
/// For example: `my-project`.
pub async fn sample(project_id: &str) -> anyhow::Result<()> {
Introduce a few use declarations to make the example more readable:
/// # Parameters
/// - `project_id`: the id of a Google Cloud project, or its numeric ID.
/// For example: `my-project`.
pub async fn sample(project_id: &str) -> anyhow::Result<()> {
Initialize the default tracing subscriber:
tracing_subscriber::fmt::init();
Initialize a client with tracing enabled. Note the call to .with_tracing():
let client = SecretManagerService::builder()
.with_tracing()
.build()
.await?;
Then use the client to send a request:
let mut items = client
.list_secrets()
.set_parent(format!("projects/{project_id}"))
.by_item();
println!("listing all secrets in project {project_id}");
while let Some(secret) = items.next().await.transpose()? {
println!(" {secret:?}");
}
println!("DONE");
Expected output
The output (slightly edited for readability) will include a line such as:
2025-11-03T14:17:31.759452Z INFO list_secrets{self=SecretManagerService ...
This line includes the request:
req=ListSecretsRequest { parent: "projects/... }
and the response:
return=Ok(Response { parts: ..., body: ListSecretsResponse { ...
More information
The default subscriber created via tracing_subscriber::fmt::init() can be
configured dynamically using the RUST_LOG environment variable. See
its documentation for details.