GCP - Cloud Functions using python stdout

Viewed 666

Using cloud functions I found that I can´t get print() output.

Example function:

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        print('test print!')
        return f'Hello World!'

Using the TEST tool from GCP y get Hello World! result, but I can't see test print! output.

Where/How can I found it?

Thanks in advance.

2 Answers

Log messages are sent to Cloud Logging. There are several methods to view/read those logs.

This link takes you to the Google Cloud Console Logs Viewer. You can then select "Cloud Functions" to see log entries.

You can also see the logs for a function in the Google Cloud Console -> Compute -> Cloud Functions. Select the "LOGS" tab.

Writing, Viewing, and Responding to Logs

Verify that your function has an IAM Role granting rights to write to Cloud Logging.

Logging Access Control Guide

You find your print statement in the Logs tab of Cloud Functions:

logging inside Cloud Functions


OR you can find it in Cloud Logging.
To find your message quickly, it helps to filter on: resource.type="cloud_function"


cloud functions print statement in Cloud Logging

If you are using 2nd generation Cloud Functions then filter in Cloud Logging on:
resource.type="cloud_run_revision"

Related