This document describes how to enable logging in Rust. Logging requests and responses to the console can make it easier to troubleshoot applications.
Prerequisites
This guide uses the Secret Manager API, which must be enabled before you begin. To learn how to enable services, follow the service quickstart.
For complete setup instructions for the Rust libraries, see Getting started with Rust.
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 crate separates 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.
Add a dependency on the
tracing-subscribercrate:cargo add tracing tracing-subscriberWrite a function that receives the project ID as a function parameter:
Introduce a few use declarations to make the example more readable:
Initialize the default tracing subscriber:
Initialize a client with tracing enabled. Note the call to
.with_tracing():Then use the client to send a request:
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/... }
The response:
return=Ok(Response { parts: ..., body: ListSecretsResponse { ...
More information
The default subscriber created using tracing_subscriber::fmt::init() can be
configured dynamically using the RUST_LOG environment variable. See
its documentation for details.