3.11.0-rc 3.10.0 (latest) 3.9.0 3.8.0 3.7.0 3.6.0 3.5.0 3.4.0 3.3.0 3.2.0 2.48.0-rc 2.47.1 2.46.0 2.45.0 2.44.0 2.43.0 2.42.0 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.1 2.24.0 2.23.0 2.22.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.14.0 2.13.0 2.12.0 2.11.0 2.10.1 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](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1LogSink#classgoogle_1_1cloud_1_1LogSink_1a63ec26a7560bdae9657f250bb93f6a14)) to the default [LogSink](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1LogSink#classgoogle_1_1cloud_1_1LogSink_1a06247b1adf1203876402ba6a9be76a7e).
- Second, you must configure what gets logged. Typically, you initialize the `*Connection` object with a [LoggingComponentsOption](https://docs.cloud.google.com/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1LoggingComponentsOption). 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](https://docs.cloud.google.com/cpp/docs/reference/common/latest/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`.

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:

    void AppCode() {
      google::cloud::LogSink::EnableStdClog();
    }

As previously noted, this can be switched at run-time using the `GOOGLE_CLOUD_CPP_ENABLE_CLOG` environment variable.