Search code examples
python-3.xgoogle-app-enginelogginggoogle-cloud-platformstackdriver

How to group related request log entries GAE python 3.7 standard env


I'm using Google App Engine python 3.7 standard and i'm trying to group related request log entries. According to the Writing Application Logs documentation, I should:

Set the trace identifier in the LogEntry trace field of your app log entries. The expected format is projects/[PROJECT_ID]/traces/[TRACE_ID]

Where/How should use LogEntry?

The Stackdriver Logging documentation doesn't show how it's possible. Am I missing something?

Code examples would be much appreciated.

[UPDATE] Following Duck Hunt Duo advice, I tried the following, without any success:

    trace_id = request.headers.get('X-Cloud-Trace-Context', 'no_trace_id').split('/')[0]
    client = logging.Client()
    logger = client.logger('appengine.googleapis.com%2Fstdout')  # Not shown
    # logger = client.logger('projects/{}/logs/stdout'.format(GOOGLE_CLOUD_PROJECT)) # error
    # logger = client.logger('projects/{}/logs/appengine.googleapis.com%2Fstdout'.format(GOOGLE_CLOUD_PROJECT)) # error

    logger.log_text('log_message', trace=trace_id)

The log doesn't appear in the GAE service log web console


Solution

  • You might want to take a look at an answer I provided here.

    (This answer addresses how to add logging severity to Cloud Functions logs written into Stackdriver, but the basic workflow is the same)

    Quoting it:

    [...], you can still create logs with certain severity by using the Stackdriver Logging Client Libraries. Check this documentation in reference to the Python libraries, and this one for some usage-case examples.

    Notice that in order to let the logs be under the correct resource, you will have to manually configure them, see this list for the supported resource types. As well, each resource type has some required labels that need to be present in the log structure.

    Edit:

    Updating the previous answer with an example for App Engine:

    from google.cloud import logging
    from google.cloud.logging.resource import Resource
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def logger():
        log_client = logging.Client()
        log_name = 'appengine.googleapis.com%2Fstdout'
    
        res = Resource( type='gae_app',
                        labels={
                            "project_id": "MY-PROJECT-ID",
                            "module_id": "MY-SERVICE-NAME"
                           })
    
        logger = log_client.logger(log_name)
    
        logger.log_struct({"message": "message string to log"}, resource=res, severity='ERROR') # As an example log message with a ERROR warning level
    
        return 'Wrote logs to {}.'.format(logger.name)
    

    By using this code as example, and changing the resource type of the log to appengine.googleapis.com%2Fstdout should work, and change the Resource fields to be the same as in the gae_app labels described in here.