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.
filter(record)
Add new Cloud Logging data to each LogRecord as it comes in
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. If not given, will be inferred from the environment.
labels (Optional[dict]) – Additional labels to attach to logs.
stream (Optional[IO]) – Stream to be used by the handler.
close()
Closes the log handler and cleans up all Transport objects used.
emit(record)
Actually log the specified logging record.
Overrides the default emit behavior of StreamHandler.
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]) – The logging level threshold of the attached logger,
as set by the logging.Logger.setLevel() method. Defaults to
logging.INFO.
[[["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."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [3.12.1 (latest)](/python/docs/reference/logging/latest/handlers-cloud-logging)\n- [3.12.0](/python/docs/reference/logging/3.12.0/handlers-cloud-logging)\n- [3.11.3](/python/docs/reference/logging/3.11.3/handlers-cloud-logging)\n- [3.10.0](/python/docs/reference/logging/3.10.0/handlers-cloud-logging)\n- [3.9.0](/python/docs/reference/logging/3.9.0/handlers-cloud-logging)\n- [3.8.0](/python/docs/reference/logging/3.8.0/handlers-cloud-logging)\n- [3.7.0](/python/docs/reference/logging/3.7.0/handlers-cloud-logging)\n- [3.6.0](/python/docs/reference/logging/3.6.0/handlers-cloud-logging)\n- [3.5.0](/python/docs/reference/logging/3.5.0/handlers-cloud-logging)\n- [3.4.0](/python/docs/reference/logging/3.4.0/handlers-cloud-logging)\n- [3.3.1](/python/docs/reference/logging/3.3.1/handlers-cloud-logging)\n- [3.2.5](/python/docs/reference/logging/3.2.5/handlers-cloud-logging)\n- [3.1.2](/python/docs/reference/logging/3.1.2/handlers-cloud-logging)\n- [3.0.0](/python/docs/reference/logging/3.0.0/handlers-cloud-logging)\n- [2.7.2](/python/docs/reference/logging/2.7.2/handlers-cloud-logging)\n- [2.6.0](/python/docs/reference/logging/2.6.0/handlers-cloud-logging)\n- [2.5.0](/python/docs/reference/logging/2.5.0/handlers-cloud-logging)\n- [2.4.0](/python/docs/reference/logging/2.4.0/handlers-cloud-logging)\n- [2.3.1](/python/docs/reference/logging/2.3.1/handlers-cloud-logging)\n- [2.2.0](/python/docs/reference/logging/2.2.0/handlers-cloud-logging)\n- [2.1.1](/python/docs/reference/logging/2.1.1/handlers-cloud-logging)\n- [2.0.2](/python/docs/reference/logging/2.0.2/handlers-cloud-logging)\n- [1.15.3](/python/docs/reference/logging/1.15.3/handlers-cloud-logging)\n- [1.14.0](/python/docs/reference/logging/1.14.0/handlers-cloud-logging)\n- [1.13.0](/python/docs/reference/logging/1.13.0/handlers-cloud-logging)\n- [1.12.1](/python/docs/reference/logging/1.12.1/handlers-cloud-logging) \n\nCloud Logging Handler\n=====================\n\nPython [`logging`](https://docs.python.org/3/library/logging.html#module-logging) handlers for Cloud Logging.\n\n### *class* google.cloud.logging_v2.handlers.handlers.CloudLoggingFilter(project=None, default_labels=None)\n\nBases: [`logging.Filter`](https://docs.python.org/3/library/logging.html#logging.Filter)\n\nPython standard `logging` Filter class to add Cloud Logging\ninformation to each LogRecord.\n\nWhen attached to a LogHandler, each incoming log will be modified\nto include new Cloud Logging relevant data. This data can be manually\noverwritten using the extras argument when writing logs.\n\nInitialize a filter.\n\nInitialize with the name of the logger which, together with its\nchildren, will have its events allowed through the filter. If no\nname is specified, allow every event.\n\n#### filter(record)\n\nAdd new Cloud Logging data to each LogRecord as it comes in\n\n### *class* google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler(client, \\*, name: str = 'python', transport: typing.Type\\[google.cloud.logging_v2.handlers.transports.base.Transport\\] = \\\u003cclass 'google.cloud.logging_v2.handlers.transports.background_thread.BackgroundThreadTransport'\\\u003e, resource: typing.Optional\\[google.cloud.logging_v2.resource.Resource\\] = None, labels: typing.Optional\\[dict\\] = None, stream: typing.Optional\\[typing.IO\\] = None, \\*\\*kwargs)\n\nBases: [`logging.StreamHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler)\n\nHandler that directly makes Cloud Logging API calls.\n\nThis is a Python standard `logging` handler using that can be used to\nroute Python standard logging messages directly to the Stackdriver\nLogging API.\n\nThis handler is used when not in GAE or GKE environment.\n\nThis handler supports both an asynchronous and synchronous transport.\n\nExample: \n\n import logging\n import google.cloud.logging\n from google.cloud.logging_v2.handlers import https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler.html\n\n client = google.cloud.logging.https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html()\n handler = CloudLoggingHandler(client)\n\n cloud_logger = logging.getLogger('cloudLogger')\n cloud_logger.setLevel(logging.INFO)\n cloud_logger.addHandler(handler)\n\n cloud_logger.error('bad news') # API call\n\n- **Parameters**\n\n - **client** ([*Client*](/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)) -- The authenticated Google Cloud Logging client for this\n handler to use.\n\n - **name** ([*str*](https://docs.python.org/3/library/stdtypes.html#str)) -- the name of the custom log in Cloud Logging.\n Defaults to 'python'. The name of the Python logger will be represented\n in the `python_logger` field.\n\n - **transport** (*Transport* ) -- Class for creating new transport objects. It should\n extend from the base [`Transport`](/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.base.Transport) type and\n implement :meth\\`.Transport.send\\`. Defaults to\n [`BackgroundThreadTransport`](/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.background_thread.BackgroundThreadTransport). The other\n option is [`SyncTransport`](/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.sync.SyncTransport).\n\n - **resource** ([*Resource*](/python/docs/reference/logging/latest/resource#google.cloud.logging_v2.resource.Resource)) -- Resource for this Handler. If not given, will be inferred from the environment.\n\n - **labels** (*Optional* *\\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#dict)*\\]*) -- Additional labels to attach to logs.\n\n - **stream** (*Optional* *\\[* *IO* *\\]*) -- Stream to be used by the handler.\n\n#### close()\n\nCloses the log handler and cleans up all Transport objects used.\n\n#### emit(record)\n\nActually log the specified logging record.\n\nOverrides the default emit behavior of `StreamHandler`.\n\nSee \u003chttps://docs.python.org/2/library/logging.html#handler-objects\u003e\n\n- **Parameters**\n\n **record** ([*logging.LogRecord*](https://docs.python.org/3/library/logging.html#logging.LogRecord)) -- The record to be logged.\n\n#### flush()\n\nForces the Transport object to submit any pending log records.\n\nFor SyncTransport, this is a no-op.\n\n### google.cloud.logging_v2.handlers.handlers.DEFAULT_LOGGER_NAME(*= 'python* )\n\nDefaults for filtering out noisy loggers\n\n### google.cloud.logging_v2.handlers.handlers.EXCLUDED_LOGGER_DEFAULTS(*= ('google.api_core.bidi', 'werkzeug'* )\n\nExclude internal logs from propagating through handlers\n\n### google.cloud.logging_v2.handlers.handlers.setup_logging(handler, \\*, excluded_loggers=('google.api_core.bidi', 'werkzeug'), log_level=20)\n\nAttach a logging handler to the Python root logger\n\nExcludes loggers that this library itself uses to avoid\ninfinite recursion.\n\nExample: \n\n import logging\n import google.cloud.logging\n from google.cloud.logging_v2.handlers import https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler.html\n\n client = google.cloud.logging.https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html()\n handler = CloudLoggingHandler(client)\n google.cloud.logging.https://cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.html.setup_logging(handler)\n logging.getLogger().setLevel(logging.DEBUG)\n\n logging.error('bad news') # API call\n\n- **Parameters**\n\n - **handler** (*logging.handler*) -- the handler to attach to the global handler\n\n - **excluded_loggers** (*Optional* *\\[* *Tuple* *\\[* [*str*](https://docs.python.org/3/library/stdtypes.html#str)*\\]* *\\]*) -- The loggers to not attach the handler\n to. This will always include the loggers in the\n path of the logging client itself.\n\n - **log_level** (*Optional* *\\[* [*int*](https://docs.python.org/3/library/functions.html#int)*\\]* ) -- The logging level threshold of the attached logger,\n as set by the [`logging.Logger.setLevel()`](https://docs.python.org/3/library/logging.html#logging.Logger.setLevel) method. Defaults to\n `logging.INFO`."]]