[PYTHON] Google Cloud Functions log output (and addicted)

Thing you want to do

--Language: Python --I want to output logs to Stack Driver Logging with Google Cloud Functions. --Looking at the docs, it says print"

What i did

--Use google-cloud-logging --Added google-cloud-logging == 1.14.0 to requirements.txt

# Imports the Google Cloud client library
import logging
from google.cloud import logging as glogging

client = glogging.Client(project=os.environ['PROJECT_ID'])
handler = client.get_default_handler()
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(handler)

def test_method(request):

  print('== start ==')

  try:
    cloud_logger.info('Info Message')
    cloud_logger.warn('Warn Message')
    raise Exception
  except Exception:
    cloud_logger.error('Error Message')
    raise Exception

  print('== finish ==')

Run, but no log

――Nothing comes out.

1_test.png 2_log.png

Cause (imagination)

――Even if you do print, the result does not change ... --Apparently, Google Cloud Functions does not output the log up to that point when it crashes (= abnormal termination) --Is there a description somewhere in the document ... --So if you get an Exception, catch it and finally change it to sys.exit ()

import sys
#Omission
  try:
    cloud_logger.info('Info Message')
    cloud_logger.warn('Warn Message')
    raise Exception
  except Exception:
    cloud_logger.error('Error Message')
    sys.exit()

  print('== finish ==')

Rerun, and error

3_log.png

Tona

Google Cloud Functions service account

--The documentation states that the default service account for Google Cloud Functions is [email protected] (App Engine default service account). Apparently he has the role of project editor. - https://cloud.google.com/functions/docs/concepts/iam?hl=ja#runtime_service_account ――When you look at SA, that kind of thing is certainly born. ――When you look at your environment ... No! ――It seems that it was gone for some reason

solution

--Create a dedicated SA and link it to Google Cloud Functions. ――Logs are now displayed properly --Why are the error levels only info and error ... 4_log.png

Looking back

--Read the document properly ――I may not have written it ――Let's create and use an SA with the necessary privileges without using the default SA by wearing it sideways. --Cloud Functions ** Do not drop **. End normally --If you want to monitor the operation status, look at the number of error messages in the log instead of the number of errors (Is there only ...?)

Recommended Posts

Google Cloud Functions log output (and addicted)
Try using Python with Google Cloud Functions
Output python log to both console and file
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Speech transcription procedure using Python and Google Cloud Speech API
Run Google Cloud Functions locally with Cloud Native Build packs
Higher-order functions and decorators
python input and output
Anonymous and map functions
Upload and delete files to Google Cloud Storages with django-storage