Integration with logging Standard Library

We recommend that you use google-cloud-logging to integrate with the Python logging standard library. This way, you can write logs using Python standards, and still have your logs appear in Google Cloud Logging.

Automatic Configuration

To integrate google-cloud-logging with the standard logging module, call setup_logging() on a Client instance.

# Imports the Cloud Logging client library

# Instantiates a client
client =

# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher

This setup_logging() function chooses the best configurations for the environment your code is running on. For more information, see the Google Cloud Logging documentation.

Manual Handler Configuration

Automatic Configuration automatically determines the appropriate handler for the environment. To specify the handler yourself, construct an instance manually and pass it in as an argument to setup_logging():

from import CloudLoggingHandler
from import setup_logging

handler = CloudLoggingHandler(client)

There are two supported handler classes to choose from:

  • CloudLoggingHandler:

      * Sends logs directly to Cloud Logging over the network ([gRPC or HTTP](
      * Logs are transmitted according to a Transport class
      * This is the default handler on most environments, including local development
  • StructuredLogHandler:

      * Outputs logs as [structured JSON](

    to standard out, to be read and parsed by a GCP logging agent

      * This is the default handler on Kubernetes Engine, Cloud Functions and Cloud Run

Handler classes can also be specified via dictConfig:


client =

    "version": 1,
    "handlers": {
        "cloud_logging": {
            "class": "",
            "client": client,
        "structured_log": {
            "class": ""
    "root": {"handlers": ["console"], "level": "WARNING"},
    "loggers": {
        "my_logger": {"handlers": ["cloud_logging"], "level": "INFO"},
        "my_other_logger": {"handlers": ["structured_log"], "level": "INFO"},

Note that since CloudLoggingHandler requires an already initialized Client, you must initialize a client and include it in the dictConfig entry for a CloudLoggingHandler.

Standard Library

After you setup the Google Cloud Logging library with the Python logging standard library, you can send logs with the standard logging library as you normally would:

# Imports Python standard library logging
import logging

# The data to log
text = "Hello, world!"

# Emits the data using the standard logging module

For more information on using the Python logging standard library, see the logging documentation

Logging JSON Payloads

Although the Python logging standard library expects all logs to be strings, Google Cloud Logging allows JSON payload data.

To write JSON logs using the standard library integration, do one of the following:

  1. Use the json_fields extra argument:
import logging

data_dict = {"hello": "world"}"message field", extra={"json_fields": data_dict})
  1. Log a JSON-parsable string:
import logging
import json

data_dict = {"hello": "world"}

Automatic Metadata Detection

The Google Cloud Logging library attempts to detect and attach additional LogEntry fields . The following fields are currently supported:

  • labels

  • trace*

  • span_id*

  • trace_sampled*

  • http_request*

  • source_location

  • resource

  • json_fields

NOTE: Fields marked with “*” require a supported Python web framework.

Manual Metadata Using the extra Argument

The Python logging standard library accepts an “extra” argument when writing logs. You can use this argument to populate LogRecord objects with user-defined key-value pairs. Google Cloud Logging uses the extra field as a way to pass in additional metadata to populate LogEntry fields.

my_labels = {"foo": "bar"}
my_http = {"requestUrl": "localhost"}
my_trace = "01234"
    "hello", extra={"labels": my_labels, "http_request": my_http, "trace": my_trace}

All of the LogEntry fields that can be autodetected can also be set manually through the extra argument. Fields sent explicitly through the extra argument override any automatically detected fields.

CloudLoggingHandler Transports

Transport classes define how the CloudLoggingHandler transports logs over the network to Google Cloud. There are two Transport implementations (defined as subclasses of transports.base.Transport):

You can set a Transport class by passing it as an argument when initializing CloudLoggingHandler manually.

You can use both transport options over gRPC or HTTP.

NOTE: StructuredLogHandler prints logs as formatted JSON to standard output, and does not use a Transport class.