Using Python Standard Logging

You can have the Python standard logging send logs to Stackdriver Logging by using the Google Cloud Python SDK logging handlers.

Installing the library

To install the Google Cloud Client library for Python, see Installing the client library. Once installed, this library includes the core code to interact with the Logging API as well as the Stackdriver Logging handlers.

Stackdriver Logging handlers

To attach the Stackdriver Logging handlers to your root Python logger, use the setup_logging helper method on your Logging client:

if not app.testing:
    client =['PROJECT_ID'])
    # Attaches a Google Stackdriver logging handler to the root logger

After setup_logging is called, the following code will report logs to Stackdriver Logging. On App Engine or Container Engine, it will send logs to their respective resource types, or in the general case, logs will apear under the "python" log in the Global resource type:

import logging
logging.error('This is an error')

You can also attach the Stackdriver Logging handlers to non-root loggers, and use a variety of synchronous and asynchronous transport options, such as HTTP, gRPC, or the Stackdriver logging agent. For more information on how to use these options, see integration with Python standard logging usage docs.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Stackdriver Logging