Enable logging

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:

  1. Add the swift-log package and its Logging product 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-log
    

    Replace TARGET_NAME with the name of your target.

  2. Add the imports required to use the client library and Logging:

    import GoogleCloudSecretManagerV1
    import GoogleGax
    import Logging

  3. Write a function that receives the project ID as a parameter:

    func sample(projectId: String) async throws {

  4. Create a Logger and set its logLevel to .debug. Because the client libraries emit request, response, and error logs at the .debug level, a logger with the default .info level doesn't output these messages:

    var logger = Logger(label: "com.example.my-app")
    logger.logLevel = .debug

  5. Initialize a client with logging enabled by setting logger on ClientOptions. Because Logger has value semantics, configure logLevel (and any custom metadata) before passing logger to ClientOptions; modifying logger afterward doesn't affect the client:

    let client = try SecretManagerServiceClient(
      ClientOptions().with {
        $0.logger = logger
      })

  6. Use the client to send a request:

    let secrets = client.listSecretsByItems(
      request: ListSecretsRequest().with { $0.parent = "projects/\(projectId)" })
    for try await item in secrets {
      print("  \(item)")
    }

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 its enter and success or error log entries

Complete code

The following example shows the complete code for this document:

import GoogleCloudSecretManagerV1
import GoogleGax
import Logging

func sample(projectId: String) async throws {
  var logger = Logger(label: "com.example.my-app")
  logger.logLevel = .debug
  let client = try SecretManagerServiceClient(
    ClientOptions().with {
      $0.logger = logger
    })
  let secrets = client.listSecretsByItems(
    request: ListSecretsRequest().with { $0.parent = "projects/\(projectId)" })
  for try await item in secrets {
    print("  \(item)")
  }
}

What's next