An overview of logging in the Google Cloud C++ client libraries.
Introduction
The client libraries never use logging to report errors, but logging can be enabled to help troubleshoot problems when the last error message does not provide a good enough indication of the root cause.
In general, we abide by the following principles:
Logging should be controlled by the application developer. Unless explicitly instructed, the libraries produce no output to the console, except to emit a message to std::clog immediately before a GCP_LOG(FATAL) terminates the process.
Logging should have very low cost:
It should be possible to disable logs at compile time. They should disappear as-if there were #ifdef/#endif directives around them.
A log line at a disabled log level should be about as expensive as an extra if() statement. At the very least it should not incur additional memory allocations or locks.
It should be easy to log complex objects.
The logging framework should play well with the C++ iostream classes.
The application should be able to intercept log records and re-direct them to their own logging framework.
Enabling logs
The application needs to do two things to enable logging:
First, to configure the destination of the logs you must add a backend (see AddBackend) to the default LogSink.
Second, you must configure what gets logged. Typically, you initialize the *Connection object with a LoggingComponentsOption. Consult the documentation for each *Client class to find what tracing components are available.
At run-time, setting the GOOGLE_CLOUD_CPP_ENABLE_CLOG to a non-empty value configures a LogBackend that uses std::clog. Likewise, setting the GOOGLE_CLOUD_CPP_ENABLE_TRACING=a,b will enable tracing for components a and b across all client objects. The most common components are auth, rpc, and rpc-streams.
Note that while std::clog is buffered, the framework will flush any log message at severity WARNING or higher.
Example: Logging From Library
Use the GCP_LOG() macro to log from a Google Cloud Platform C++ library:
void LibraryCode(ComplexThing const& thing) {
GCP_LOG(INFO) << "I am here";
if (thing.is_bad()) {
GCP_LOG(ERROR) << "Poor thing is bad: " << thing;
}
}
Example: Enable Logs to std::clog
To enable logs to std::clog the application can call:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[],[],null,["Version 2.38.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/common/latest/logging)\n- [2.41.0](/cpp/docs/reference/common/2.41.0/logging)\n- [2.40.0](/cpp/docs/reference/common/2.40.0/logging)\n- [2.39.0](/cpp/docs/reference/common/2.39.0/logging)\n- [2.38.0](/cpp/docs/reference/common/2.38.0/logging)\n- [2.37.0](/cpp/docs/reference/common/2.37.0/logging)\n- [2.36.0](/cpp/docs/reference/common/2.36.0/logging)\n- [2.35.0](/cpp/docs/reference/common/2.35.0/logging)\n- [2.34.0](/cpp/docs/reference/common/2.34.0/logging)\n- [2.33.0](/cpp/docs/reference/common/2.33.0/logging)\n- [2.32.0](/cpp/docs/reference/common/2.32.0/logging)\n- [2.31.0](/cpp/docs/reference/common/2.31.0/logging)\n- [2.30.0](/cpp/docs/reference/common/2.30.0/logging)\n- [2.29.0](/cpp/docs/reference/common/2.29.0/logging)\n- [2.28.0](/cpp/docs/reference/common/2.28.0/logging)\n- [2.27.0](/cpp/docs/reference/common/2.27.0/logging)\n- [2.26.0](/cpp/docs/reference/common/2.26.0/logging)\n- [2.25.1](/cpp/docs/reference/common/2.25.1/logging)\n- [2.24.0](/cpp/docs/reference/common/2.24.0/logging)\n- [2.23.0](/cpp/docs/reference/common/2.23.0/logging)\n- [2.22.1](/cpp/docs/reference/common/2.22.1/logging)\n- [2.21.0](/cpp/docs/reference/common/2.21.0/logging)\n- [2.20.0](/cpp/docs/reference/common/2.20.0/logging)\n- [2.19.0](/cpp/docs/reference/common/2.19.0/logging)\n- [2.18.0](/cpp/docs/reference/common/2.18.0/logging)\n- [2.17.0](/cpp/docs/reference/common/2.17.0/logging)\n- [2.16.0](/cpp/docs/reference/common/2.16.0/logging)\n- [2.15.1](/cpp/docs/reference/common/2.15.1/logging)\n- [2.14.0](/cpp/docs/reference/common/2.14.0/logging)\n- [2.13.0](/cpp/docs/reference/common/2.13.0/logging)\n- [2.12.0](/cpp/docs/reference/common/2.12.0/logging)\n- [2.11.0](/cpp/docs/reference/common/2.11.0/logging)\n- [2.10.1](/cpp/docs/reference/common/2.10.1/logging) \n\nLogging Components\n==================\n\nAn overview of logging in the Google Cloud C++ client libraries.\n\n### Introduction\n\nThe client libraries never use logging to report errors, but logging can be enabled to help troubleshoot problems when the last error message does not provide a good enough indication of the root cause.\n\nIn general, we abide by the following principles:\n\n- Logging should be controlled by the application developer. Unless explicitly instructed, the libraries produce no output to the console, except to emit a message to `std::clog` immediately before a `GCP_LOG(FATAL)` terminates the process.\n- Logging should have very low cost:\n - It should be possible to disable logs at compile time. They should disappear as-if there were `#ifdef`/`#endif` directives around them.\n - A log line at a disabled log level should be about as expensive as an extra `if()` statement. At the very least it should not incur additional memory allocations or locks.\n- It should be easy to log complex objects.\n- The logging framework should play well with the C++ iostream classes.\n- The application should be able to intercept log records and re-direct them to their own logging framework.\n\n### Enabling logs\n\nThe application needs to do two things to enable logging:\n\n- First, to configure the destination of the logs you must add a backend (see [AddBackend](/cpp/docs/reference/common/2.38.0/classgoogle_1_1cloud_1_1LogSink#classgoogle_1_1cloud_1_1LogSink_1a63ec26a7560bdae9657f250bb93f6a14)) to the default [LogSink](/cpp/docs/reference/common/2.38.0/classgoogle_1_1cloud_1_1LogSink#classgoogle_1_1cloud_1_1LogSink_1a06247b1adf1203876402ba6a9be76a7e).\n- Second, you must configure what gets logged. Typically, you initialize the `*Connection` object with a [LoggingComponentsOption](/cpp/docs/reference/common/2.38.0/structgoogle_1_1cloud_1_1LoggingComponentsOption). Consult the documentation for each `*Client` class to find what tracing components are available.\n\nAt run-time, setting the `GOOGLE_CLOUD_CPP_ENABLE_CLOG` to a non-empty value configures a [LogBackend](/cpp/docs/reference/common/2.38.0/classgoogle_1_1cloud_1_1LogBackend) that uses `std::clog`. Likewise, setting the `GOOGLE_CLOUD_CPP_ENABLE_TRACING=a,b` will enable tracing for components `a` and `b` across **all** client objects. The most common components are `auth`, `rpc`, and `rpc-streams`.\n\nNote that while `std::clog` is buffered, the framework will flush any log message at severity `WARNING` or higher.\n\n### Example: Logging From Library\n\nUse the `GCP_LOG()` macro to log from a Google Cloud Platform C++ library: \n\n void LibraryCode(ComplexThing const& thing) {\n GCP_LOG(INFO) \u003c\u003c \"I am here\";\n if (thing.is_bad()) {\n GCP_LOG(ERROR) \u003c\u003c \"Poor thing is bad: \" \u003c\u003c thing;\n }\n }\n\n### Example: Enable Logs to std::clog\n\nTo enable logs to `std::clog` the application can call: \n\n void AppCode() {\n google::cloud::LogSink::EnableStdClog();\n }\n\nAs previously noted, this can be switched at run-time using the `GOOGLE_CLOUD_CPP_ENABLE_CLOG` environment variable."]]