Migrating to Cloud Logging

With just a few lines of code, you can update your app to use Cloud Logging and achieve nearly the same filtering and log correlation features that were available with the App Engine logging service.

To migrate to Cloud Logging, attach the Cloud Logging handler to the Python root logger, then continue using the Python logging module to write log entries.

Key differences between App Engine logging and Cloud Logging

  • App Engine logging requires you use the Python logging module to write app log entries. Cloud Logging supports all of the following approaches:

    • Use the Cloud Logging client library directly.

    • Write entries to stdout or stderr. These entries appear in the Logs Explorer, but to enable filtering and correlation with request logs you need to format the entries as a JSON object and provide specific metadata. For more information about this approach, see Structured logging.

  • With App Engine logging, the Logs Explorer assigns a severity level to request logs, and the severity level reflects the highest severity of any app log entry that is correlated with the request. For example, if a request results in your app emitting a warning log entry, the Logs Explorer will display a warning icon next to the request log entry. When you expand the request entry, you see the warning log entry nested within the request entry.

    With Cloud Logging, the Log Viewer does not assign a severity level to request logs. When you expand a request entry, you'll see app entries nested within the request entry, and those app entries will show a severity icon. But the request entry itself does not show a severity icon and cannot be filtered by severity level.

  • Quotas and data retention are the same for both App Engine and Cloud Logging, but if you exceed the free quota of logs data, Cloud Logging pricing may be different from App Engine logs pricing.

Before you start migrating

  1. If you have not done so already, set up your Python development environment to use a Python version that is compatible with Google Cloud, and install testing tools for creating isolated Python environments.

  2. Enable the Cloud Logging API in the project that contains your app.

    Enable the API

  3. Make sure your app has permission to write logs.

    By default, your app's default service account has permission to write logs.

    If your app uses a different service account or user account, or if you have changed permissions for the default service account, make sure the account you use has permission to write logs.

Overview of the migration process

To migrate your Python app to use Cloud Logging instead of App Engine logging:

  1. Install the Cloud Client Library for Cloud Logging.

  2. Write logs with the standard Python logging module.

  3. Deploy your app to App Engine and confirm logging works as expected.

Installing the Cloud Client Library for Cloud Logging

Install and update your configuration files:

  1. Update the app.yaml file. Follow the instructions for your version of Python:

    Python 2

    For Python 2 apps, add the latest versions of grpcio, setuptools, and ssl libraries.

    The following is an example app.yaml file:

    runtime: python27
    threadsafe: yes
    api_version: 1
    - name: grpcio
      version: latest
    - name: setuptools
      version: latest
    - name: ssl
       version: latest

    Some client libraries don't need the SSL library. If you don't include the SSL library for a client library that needs it, you'll see an SSL error in the Logs Explorer when your app receives a request.

    Python 3

    For Python 3 apps, delete all lines except for the runtime element. For example:

    runtime: python310 # or another support version

    The Python 3 runtime installs libraries automatically, so you do not need to specify built-in libraries from the previous Python 2 runtime. If your Python 3 app is using other legacy bundled services when migrating, leave the app.yaml file as is.

  2. Update the requirements.txt file. Follow the instructions for your version of Python:

    Python 2

    Add the Cloud Client Libraries for Cloud Logging to your list of dependencies in the requirements.txt file.


    We recommend you use the 1.14.0 version of the Cloud Logging client library since it supports Python 2.7 apps.

    Then run pip install -t lib -r requirements.txt to update the list of available libraries for your app.

    Python 3

    Add the Cloud Client Libraries for Cloud Logging to your list of dependencies in the requirements.txt file.


    App Engine automatically installs these dependencies during app deployment in the Python 3 runtime, so delete the lib folder if one exists.

  3. For Python 2 apps, if your app is using built-in or copied libraries specified in the lib directory, you must specify those paths in the appengine_config.py file:

    import pkg_resources
    from google.appengine.ext import vendor
    # Set PATH to your libraries folder.
    PATH = 'lib'
    # Add libraries installed in the PATH folder.
    # Add libraries to pkg_resources working set to find the distribution.

    The appengine_config.py file in the preceding example assumes that the the lib folder is located in the current working directory. If you can't guarantee that lib will always be in the current working directory, specify the full path to the lib folder. For example:

    import os
    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')

Write logs with the standard Python logging module

In each file that writes log entries:

  1. Import the Cloud Logging client library.
  2. Instantiate the Cloud Logging client.
  3. Attach the Cloud Logging handler to the Python root logger by calling the setup_logging method.

  4. Run the Cloud Logging client's setup_logging() method, which attaches its default listener as the logging handler for the Python root logger.

For example:

# Imports the Cloud Logging client library
import google.cloud.logging

# Instantiates a client
client = google.cloud.logging.Client()

# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher

Once the handler is attached, any logs at, by default, INFO level or higher which are emitted in your application will be sent to Logging:

# Imports Python standard library logging
import logging

# The data to log
text = "Hello, world!"

# Emits the data using the standard logging module

Deploying your app

Once you are ready to deploy your app, you should:

  1. Test the app on App Engine.

    The only way to test your updates and confirm that severity levels are assigned and app entries are correlated with request entries is to deploy your app to the Python 3 runtime.

    If you deploy your updates to the Python 2 runtime, the App Engine logging service will listen for entries from the Python root logger and format them appropriately for the Logs Explorer.

    If you run your app locally, Cloud Logging does not use the deprecated AppEngineHandler, and App Engine cannot write request entries.

  2. View the Logs Explorer and confirm that severity levels are assigned to your app's entries. Expand a request entry and confirm that entries written by your app while it was processing the request appear nested below the request entry.

  3. If the app runs without errors, use traffic splitting to slowly ramp up traffic for your updated app. Monitor the app closely for any issues before routing more traffic to the updated app.