This document describes how to enable debug logging in the Google Cloud client libraries for Swift. Logging requests, responses, and errors to the console helps you troubleshoot applications.
Before you begin
This document uses the Secret Manager API. To enable this API, follow
the Secret Manager quickstart. Verify that your
principal has permissions to list secrets, such as the Secret Manager Viewer
Identity and Access Management (IAM) role (roles/secretmanager.viewer).
For complete setup instructions for the Swift client libraries, see Quickstart: Get started with Swift.
Configure debug logging
The Swift client libraries use Apple's
swift-log package on GitHub (Logging module) to emit
structured, contextual diagnostic messages. The swift-log package separates
the logging API (Logger) from the components that collect and format log
messages (LogHandler). By default, swift-log uses StreamLogHandler to
write log messages to standard error, and many other
logging backends are available on Swift.org.
To configure debug logging, follow these steps:
Add the
swift-logpackage and itsLoggingproduct to your target dependencies:swift package add-dependency \ https://github.com/apple/swift-log.git --from 1.12.0 swift package add-target-dependency \ Logging TARGET_NAME --package swift-logReplace
TARGET_NAMEwith the name of your target.Add the imports required to use the client library and
Logging:Write a function that receives the project ID as a parameter:
Create a
Loggerand set itslogLevelto.debug. Because the client libraries emit request, response, and error logs at the.debuglevel, a logger with the default.infolevel doesn't output these messages:Initialize a client with logging enabled by setting
loggeronClientOptions. BecauseLoggerhas value semantics, configurelogLevel(and any custom metadata) before passingloggertoClientOptions; modifyingloggerafterward doesn't affect the client:Use the client to send a request:
Expected output
When the client executes an RPC, it logs an enter message before sending the
request. Afterward, it logs either a success message with the response or an
error message if the call fails. These log entries wrap the logical RPC call
across any retry attempts rather than logging each individual attempt. When
iterating over a paginated sequence such as listSecretsByItems, each fetched
page issues a separate listSecrets RPC with its own enter and success or
error log entries.
The output (formatted for readability) includes lines such as the following:
2026-09-30T18:00:00+0000 debug com.example.my-app:
gcp.artifact.id=google-cloud-secretmanager-v1
gcp.client.service=secretmanager
gcp.experimental.swift.client=SecretManagerServiceClient
gcp.experimental.swift.method=listSecrets
gcp.experimental.swift.request.id=550E8400-E29B-41D4-A716-446655440000
[GoogleCloudSecretManagerV1] enter : ListSecretsRequest(parent: "projects/PROJECT_ID", ...) RequestOptions(...)
On success, the client logs the response:
2026-09-30T18:00:01+0000 debug com.example.my-app:
gcp.artifact.id=google-cloud-secretmanager-v1
gcp.client.service=secretmanager
gcp.experimental.swift.client=SecretManagerServiceClient
gcp.experimental.swift.method=listSecrets
gcp.experimental.swift.request.id=550E8400-E29B-41D4-A716-446655440000
[GoogleCloudSecretManagerV1] success: ListSecretsRequest(...) RequestOptions(...) ListSecretsResponse(...)
If the request fails, the client logs the error details:
2026-09-30T18:00:01+0000 debug com.example.my-app:
gcp.artifact.id=google-cloud-secretmanager-v1
gcp.client.service=secretmanager
gcp.experimental.swift.client=SecretManagerServiceClient
gcp.experimental.swift.method=listSecrets
gcp.experimental.swift.request.id=550E8400-E29B-41D4-A716-446655440000
[GoogleCloudSecretManagerV1] error : ListSecretsRequest(...) RequestOptions(...) http(StatusError(...))
In addition to any metadata already attached to the Logger by your
application, the client automatically attaches structured metadata to each log
entry:
gcp.artifact.id: the client library package identifier (for example,google-cloud-secretmanager-v1)gcp.client.service: the target Google Cloud service name (for example,secretmanager)gcp.experimental.swift.client: the name of the service client (for example,SecretManagerServiceClient)gcp.experimental.swift.method: the RPC method name being invoked (for example,listSecrets)gcp.experimental.swift.request.id: a UUID generated for each RPC invocation to correlate itsenterandsuccessorerrorlog entries
Complete code
The following example shows the complete code for this document:
What's next
- Learn how to configure package traits for large client libraries.
- Learn how to override the default endpoint.
- Browse the Google Cloud client libraries for Swift reference documentation.