Python Logging Module Handler

Python logging handlers for Cloud Logging.

class, default_labels=None)

Bases: logging.Filter

Python standard logging Filter class to add Cloud Logging information to each LogRecord.

When attached to a LogHandler, each incoming log will be modified to include new Cloud Logging relevant data. This data can be manually overwritten using the extras argument when writing logs.

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.


Add new Cloud Logging data to each LogRecord as it comes in

class, *, name='python', transport=<class ''>, resource=Resource(type='global', labels={}), labels=None, stream=None)

Bases: logging.StreamHandler

Handler that directly makes Cloud Logging API calls.

This is a Python standard logging handler using that can be used to route Python standard logging messages directly to the Stackdriver Logging API.

This handler is used when not in GAE or GKE environment.

This handler supports both an asynchronous and synchronous transport.


import logging
from import CloudLoggingHandler

client =
handler = CloudLoggingHandler(client)

cloud_logger = logging.getLogger('cloudLogger')

cloud_logger.error('bad news')  # API call
  • Parameters

    • client (Client) – The authenticated Google Cloud Logging client for this handler to use.

    • name (str) – the name of the custom log in Cloud Logging. Defaults to ‘python’. The name of the Python logger will be represented in the python_logger field.

    • transport (Transport) – Class for creating new transport objects. It should extend from the base Transport type and implement :meth`.Transport.send`. Defaults to BackgroundThreadTransport. The other option is SyncTransport.

    • resource (Resource) – Resource for this Handler. Defaults to global.

    • labels (Optional[dict]) – Additional labels to attach to logs.

    • stream (Optional[IO]) – Stream to be used by the handler.


Actually log the specified logging record.

Overrides the default emit behavior of StreamHandler.

See = 'python')

Exclude internal logs from propagating through handlers, *, excluded_loggers=('', 'google.auth', 'google_auth_httplib2', 'google.api_core.bidi', 'werkzeug'), log_level=20)

Attach a logging handler to the Python root logger

Excludes loggers that this library itself uses to avoid infinite recursion.


import logging
from import CloudLoggingHandler

client =
handler = CloudLoggingHandler(client)

logging.error('bad news')  # API call
  • Parameters

    • handler (logging.handler) – the handler to attach to the global handler

    • excluded_loggers (Optional[Tuple[str]]) – The loggers to not attach the handler to. This will always include the loggers in the path of the logging client itself.

    • log_level (Optional[int]) – Python logging log level. Defaults to logging.INFO.