# Cloud Logging Handler

3.16.0 (latest) 3.15.0 3.13.0 3.12.1 3.11.3 3.10.0 3.9.0 3.8.0 3.7.0 3.6.0 3.5.0 3.4.0 3.3.1 3.2.5 3.1.2 3.0.0 2.7.2 2.6.0 2.5.0 2.4.0 2.3.1 2.2.0 2.1.1 2.0.2 1.15.3 1.14.0 1.13.0 1.12.1 Python [`logging`](https://docs.python.org/3/library/logging.html#module-logging) handlers for Cloud Logging.

### *class* google.cloud.logging_v2.handlers.handlers.CloudLoggingFilter(project=None, default_labels=None)

Bases: [`logging.Filter`](https://docs.python.org/3/library/logging.html#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.

#### filter(record)

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

### *class* google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler(client, \*, name: str = 'python', transport: typing.Type\[google.cloud.logging_v2.handlers.transports.base.Transport\] = \<class 'google.cloud.logging_v2.handlers.transports.background_thread.BackgroundThreadTransport'\>, resource: typing.Optional\[google.cloud.logging_v2.resource.Resource\] = None, labels: typing.Optional\[dict\] = None, stream: typing.Optional\[typing.IO\] = None, \*\*kwargs)

Bases: [`logging.StreamHandler`](https://docs.python.org/3/library/logging.handlers.html#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.

Example:

    import logging
    import google.cloud.logging
    from google.cloud.logging_v2.handlers import https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler.html

    client = google.cloud.logging.https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html()
    handler = CloudLoggingHandler(client)

    cloud_logger = logging.getLogger('cloudLogger')
    cloud_logger.setLevel(logging.INFO)
    cloud_logger.addHandler(handler)

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

- **Parameters**

  - **client** ([*Client*](https://docs.cloud.google.com/python/docs/reference/logging/latest/client#google.cloud.logging_v2.client.Client)) -- The authenticated Google Cloud Logging client for this
    handler to use.

  - **name** ([*str*](https://docs.python.org/3/library/stdtypes.html#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`](https://docs.cloud.google.com/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.base.Transport) type and
    implement :meth\`.Transport.send\`. Defaults to
    [`BackgroundThreadTransport`](https://docs.cloud.google.com/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.background_thread.BackgroundThreadTransport). The other
    option is [`SyncTransport`](https://docs.cloud.google.com/python/docs/reference/logging/latest/transport#google.cloud.logging_v2.handlers.transports.sync.SyncTransport).

  - **resource** ([*Resource*](https://docs.cloud.google.com/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.

  - **labels** (*Optional* *\[* [*dict*](https://docs.python.org/3/library/stdtypes.html#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`.

See <https://docs.python.org/2/library/logging.html#handler-objects>

- **Parameters**

  **record** ([*logging.LogRecord*](https://docs.python.org/3/library/logging.html#logging.LogRecord)) -- The record to be logged.

#### flush()

Forces the Transport object to submit any pending log records.

For SyncTransport, this is a no-op.

### google.cloud.logging_v2.handlers.handlers.DEFAULT_LOGGER_NAME(*= 'python* )

Defaults for filtering out noisy loggers

### google.cloud.logging_v2.handlers.handlers.EXCLUDED_LOGGER_DEFAULTS(*= ('google.api_core.bidi', 'werkzeug'* )

Exclude internal logs from propagating through handlers

### google.cloud.logging_v2.handlers.handlers.setup_logging(handler, \*, excluded_loggers=('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.

Example:

    import logging
    import google.cloud.logging
    from google.cloud.logging_v2.handlers import https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler.html

    client = google.cloud.logging.https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client.html()
    handler = CloudLoggingHandler(client)
    google.cloud.logging.https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.handlers.handlers.html.setup_logging(handler)
    logging.getLogger().setLevel(logging.DEBUG)

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

- **Parameters**

  - **handler** (*logging.handler*) -- the handler to attach to the global handler

  - **excluded_loggers** (*Optional* *\[* *Tuple* *\[* [*str*](https://docs.python.org/3/library/stdtypes.html#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*](https://docs.python.org/3/library/functions.html#int)*\]* ) -- The logging level threshold of the attached logger,
    as set by the [`logging.Logger.setLevel()`](https://docs.python.org/3/library/logging.html#logging.Logger.setLevel) method. Defaults to
    `logging.INFO`.