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 tostd::clogimmediately before aGCP_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/#endifdirectives around them.
A log line at a disabled log level should be about as expensive as an extraif()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 (seeAddBackend) to the defaultLogSink.
Second, you must configure what gets logged. Typically, you initialize the*Connectionobject with aLoggingComponentsOption. Consult the documentation for each*Clientclass to find what tracing components are available.
At run-time, setting theGOOGLE_CLOUD_CPP_ENABLE_CLOGto a non-empty value configures aLogBackendthat usesstd::clog. Likewise, setting theGOOGLE_CLOUD_CPP_ENABLE_TRACING=a,bwill enable tracing for componentsaandbacrossallclient objects. The most common components areauth,rpc, andrpc-streams.
Note that whilestd::clogis buffered, the framework will flush any log message at severityWARNINGor higher.
Example: Logging From Library
Use theGCP_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 tostd::clogthe application can call:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis page provides documentation for logging in the Google Cloud C++ client libraries, emphasizing that logging is controlled by the application developer and does not occur by default.\u003c/p\u003e\n"],["\u003cp\u003eThe libraries offer logging capabilities to help troubleshoot issues beyond what the last error message might indicate, with a focus on minimal performance impact and no extra memory allocations.\u003c/p\u003e\n"],["\u003cp\u003eEnabling logs requires configuring a backend for log destinations and setting which components to log, typically done by initializing a \u003ccode\u003e*Connection\u003c/code\u003e object with \u003ccode\u003eLoggingComponentsOption\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eLogs can be directed to \u003ccode\u003estd::clog\u003c/code\u003e either programmatically via \u003ccode\u003eLogSink::EnableStdClog()\u003c/code\u003e or by setting the \u003ccode\u003eGOOGLE_CLOUD_CPP_ENABLE_CLOG\u003c/code\u003e environment variable at run-time.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eGCP_LOG()\u003c/code\u003e macro is used within the libraries to create log entries, and the \u003ccode\u003eGOOGLE_CLOUD_CPP_ENABLE_TRACING\u003c/code\u003e environment variable allows enabling specific components across all client objects.\u003c/p\u003e\n"]]],[],null,["Version 2.33.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.33.0/classgoogle_1_1cloud_1_1LogSink#classgoogle_1_1cloud_1_1LogSink_1a63ec26a7560bdae9657f250bb93f6a14)) to the default [LogSink](/cpp/docs/reference/common/2.33.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.33.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.33.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."]]