Currently there is no way to add the execution_id
to the logs entries. But following the approach in this answer you can easily filter the logs entries belonging to the same function execution in the Stackdriver Logging interface.
With this Cloud Function code:
import time
from google.cloud import logging
from google.cloud.logging.resource import Resource
identifier = str(time.time())
log_client = logging.Client()
log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'
resource = Resource(type="cloud_function", labels={"function_name": "yourCloudFunctionName", "region": "yourFunctionLocation"})
logger = log_client.logger(log_name.format("yourProjectId"))
def hello_world(request):
logger.log_struct({"message": "message string to log", "id": identifier}, resource=resource, severity='ERROR')
return 'Wrote logs to {}.'.format(logger.name)
Once executed open the log entry in the Strackdriver Logging interface. Display the jsonPayload
and clicking in the id
element it will display Show matching entries
. This will add the filter:
resource.type="cloud_function"
resource.labels.function_name="yourFunctionName"
jsonPayload.id="theID"
And all the logs entry belonging to this execution will be shown.
Finally, just keep in mind that Cloud Function Python 3.7 runtime is still in beta and might be subject to future changes.