You can write logs to Stackdriver Logging from Python applications by using the Python logging handler included with the Stackdriver Logging client library, or by using the client library to access the API directly.
Before you begin
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
Select or create a GCP project.
Make sure that billing is enabled for your project.
- Enable the Stackdriver Logging API.
- Prepare your environment for Python development.
Installing the library
To install the Stackdriver Logging library for Python, see Installing the client library.
Once installed, this library includes logging handlers to connect Python's standard logging module to Stackdriver, as well as an API client library to access Stackdriver Logging manually.
Connecting the library to Python logging
To send all log entries to Stackdriver by attaching the Stackdriver Logging
handler to the Python root logger, use the
setup_logging helper method:
# Imports the Google Cloud client library import google.cloud.logging # Instantiates a client client = google.cloud.logging.Client() # Connects the logger to the root logging handler; by default this captures # all logs at INFO level and higher client.setup_logging()
Using the Python root logger
Once the handler is attached, any logs at, by default, INFO level or higher which are emitted in your application will be sent to Stackdriver Logging:
# Imports Python standard library logging import logging # The data to log text = 'Hello, world!' # Emits the data using the standard logging module logging.warn(text)
If messages are logged to Stackdriver Logging from App Engine or Kubernetes Engine, the handler will send them to those environments' respective resource types; otherwise, logs will by default appear under the "python" log in the Global resource type.
Configuring the logging handler
To attach the Stackdriver logging handler to only select Python loggers, or to otherwise configure the logging handler, see the API library documentation
Using the Cloud Client Library directly
For information on using the Stackdriver Logging Cloud client library for Python directly, see Stackdriver Logging Client Libraries.
Running on Google Cloud Platform
The Stackdriver Logging library for Python can be used without needing to explicitly provide credentials.
Stackdriver Logging is automatically enabled for App Engine applications. No additional setup is required.
On Google Kubernetes Engine, you must add the
scope when creating the cluster:
gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/logging.write
When using Google Compute Engine VM instances, add the
access scope to each instance. When creating a new instance through the
Google Cloud Platform Console, you can do this in the Identity and API access section
of the Create Instance panel. Use the Compute Engine default service account
or another service account of your choice, and select Allow full access to all
Cloud APIs in the Identity and API access section. Whichever service
account you select, ensure that it has been granted the
Logs Writer role in the IAM & admin section of the
Running locally and elsewhere
To use the Stackdriver Logging library for Python outside of Google Cloud Platform, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Logging library for Python. This applies to running the agent on your own workstation, on your data center's computers, or on the VM instances of another cloud provider. For more information see Obtaining and providing service account credentials manually.
Viewing the logs
After deployment, you can view the logs in the GCP Console Logs Viewer.
For more information, see Viewing Logs.