--Language: Python
--I want to output logs to Stack Driver Logging with Google Cloud Functions.
--Looking at the docs, it says print
"
--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 ==')
――Nothing comes out.
――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 ==')
Failed to submit 3 logs.
google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission
Tona
--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
--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 ...
--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