i would like to send more expressive log entries to stackdriver logging from my app engine standard python3 app. By following the official documentation i was able to send my logs to stackdriver and it seems that the timestamp is parsed correctly.
But i'm missing the severity levels. In addition i see no way to link logs for a certain request together to a operation. Something that the java logging seems to be doing out of the box.
For reference here is my code:
import logging
import os
from flask import Flask
from google.cloud import logging as glog
app = Flask(__name__)
log_client = glog.Client(os.getenv('GOOGLE_CLOUD_PROJECT'))
# Attaches a Google Stackdriver logging handler to the root logger
log_client.setup_logging()
@app.route('/_ah/push-handlers/cloudbuild', methods=['POST'])
def pubsub_push_handle():
logging.info("stdlib info")
logging.warning("stdlib warn")
logging.error("stdlib error")
logs resulting in stackdriver:
As you can see the timestamps and message are available while the severity is strangely missing and it gets classified as 'Any'
Can someone point me in the right direction or is this level of incorporation not yet available?
Thanks for your time! Carsten
Filter the Logs Stackdriver web GUI provides a convenient interface for search which allows you to filter on data, date ranges, severity, log names by writing SQL like queries. You can search using a keyword or multiple words, which works using an “OR” operator by default.
Google Cloud provides a client library for accessing StackDriver. Following code snippets tells you how to use this client library: Now, run the script and your logger messages are written to the Cloud logging service. In order to see the logs in cloud, go to cloud console and navigate to logging -> logs.
The log names returned are in resource format; they are URL-encoded and the log names are prefixed by /projects/PROJECT_ID/logs/. Note: Because there can be many logs, it’s recommended to optimize your queries and to paginate, stream, or tail logs. To learn how to install and use the client library for Logging, see Logging client libraries .
Using Cloud Logging library for Python requires the IAM Logs Writer role on Google Cloud. Most Google Cloud environments provide this role by default. Cloud Logging is automatically enabled for App Engine, and your app's default service account has the IAM permissions by default to write log entries.
You need to create your own logger and add the google-cloud-logging
default handler to it:
import logging
from flask import Flask
from google.cloud import logging as cloudlogging
log_client = cloudlogging.Client()
log_handler = log_client.get_default_handler()
cloud_logger = logging.getLogger("cloudLogger")
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(log_handler)
app = Flask(__name__)
@app.route('/_ah/push-handlers/cloudbuild', methods=['POST'])
def pubsub_push_handle():
cloud_logger.info("info")
cloud_logger.warning("warn")
cloud_logger.error("error")
return 'OK'
Produces:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With