Skip to content

Latest commit

 

History

History
155 lines (112 loc) · 6.69 KB

std-lib-integration.rst

File metadata and controls

155 lines (112 loc) · 6.69 KB

Integration with logging Standard Library

We recommend that you use :mod:`google-cloud-logging` to integrate with the Python :mod:`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 :mod:`google-cloud-logging` with the standard :mod:`logging` module, call :meth:`~google.cloud.logging_v2.client.Client.setup_logging` on a :class:`~google.cloud.logging_v2.client.Client` instance.

.. literalinclude:: ../samples/snippets/handler.py
    :start-after: [START logging_handler_setup]
    :end-before: [END logging_handler_setup]
    :dedent: 4

This :meth:`~google.cloud.logging_v2.client.Client.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 :meth:`~google.cloud.logging_v2.handlers.setup_logging`:

.. literalinclude:: ../samples/snippets/usage_guide.py
    :start-after: [START create_cloud_handler]
    :end-before: [END create_cloud_handler]
    :dedent: 4

There are two supported handler classes to choose from:

Handler classes can also be specified via dictConfig:

.. literalinclude:: ../samples/snippets/usage_guide.py
    :start-after: [START logging_dict_config]
    :end-before: [END logging_dict_config]
    :dedent: 4

Note that since :class:`~google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler` requires an already initialized :class:`~google.cloud.logging_v2.client.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 :mod:`logging` standard library, you can send logs with the standard logging library as you normally would:

.. literalinclude:: ../samples/snippets/handler.py
    :start-after: [START logging_handler_usage]
    :end-before: [END logging_handler_usage]
    :dedent: 4

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

Logging JSON Payloads

Although the Python :mod:`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:
.. literalinclude:: ../samples/snippets/usage_guide.py
    :start-after: [START logging_extra_json_fields]
    :end-before: [END logging_extra_json_fields]
    :dedent: 4

  1. Log a JSON-parsable string:
.. literalinclude:: ../samples/snippets/usage_guide.py
    :start-after: [START logging_json_dumps]
    :end-before: [END logging_json_dumps]
    :dedent: 4


Automatic Metadata Detection

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

Manual Metadata Using the extra Argument

The Python :mod:`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.

.. literalinclude:: ../samples/snippets/usage_guide.py
    :start-after: [START logging_extras]
    :end-before: [END logging_extras]
    :dedent: 4

All of the LogEntry fields that can be :ref:`autodetected<Autodetection>` can also be set manually through the extra argument. Fields sent explicitly through the extra argument override any :ref:`automatically detected<Autodetection>` fields.

CloudLoggingHandler Transports

:doc:`Transport</transport>` classes define how the :class:`~google.cloud.logging_v2.handlers.handlers.CloudLoggingHandler` transports logs over the network to Google Cloud. There are two Transport implementations (defined as subclasses of :class:`transports.base.Transport <google.cloud.logging_v2.handlers.transports.base.Transport>`):

You can set a Transport class by passing it as an argument when :ref:`initializing CloudLoggingHandler manually.<manual handler>`

You can use both transport options over :doc:`gRPC or HTTP</grpc-vs-http>`.

Note

:class:`~google.cloud.logging_v2.handlers.structured_log.StructuredLogHandler` prints logs as formatted JSON to standard output, and does not use a Transport class.