I need to read the status of a storage trigger cloud function, the last line of the log
2021-03-09 15:47:42.908 IST function_name 6cymbohrrv6g Function execution took 11 ms, finished with status: 'ok'
This way I can determine if the function execution was a success or not.
I tried reading the logs using the below script, but it displays all the logs.
logging_client = logging.Client()
for entry in logging_client.list_entries():
timestamp = entry.timestamp.isoformat()
print("* {}: {}".format(timestamp, entry.payload))
Is there some way I can limit the logs to the current execution? Can I read the last log which displays the status of the function?
You can use and advanced filter in your logging_client.list_entries()
method. The official documentation of the Python client library for Stackdriver Logging admits this parameter.
filter (str) – a filter expression. See https://cloud.google.com/logging/docs/view/advanced_filters
For example:
from google.cloud import logging
logging_client = logging.Client()
f = 'resource.type="cloud_function" AND resource.labels.function_name="yourFunctionName" AND textPayload:"Function execution took"'
for entry in logging_client.list_entries(filter_=f):
print("{} {}".format(entry.labels['execution_id'], entry.payload))
will output:
...
gssssmirx021 Function execution took 20522 ms, finished with status code: 200
gsssn1aibbc5 Function execution took 20022 ms, finished with status code: 200
Notice the substring operator (:)
on textPayload:"Function execution took"