Describes how to output Cloud Functions logs written in Python to Cloud Logging (Stackdriver Logging).
Write as follows.
Cloud Logging records anything above the level specified by cloud_logger.setLevel
.
import logging
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client)
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.DEBUG) # defaults to WARN
# cloud_logger.setLevel(logging.INFO) # defaults to WARN
cloud_logger.addHandler(handler)
cloud_logger.debug('debug')
cloud_logger.info('info')
cloud_logger.warn('warn')
Please describe the following two in requirements.txt. (Version number is omitted)
google-cloud-logging
google-cloud-core
When I didn't write google-cloud-core
, I got the following error.
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: __init__() takes 2 positional arguments but 3 were given
Integration with Python logging module https://googleapis.dev/python/logging/latest/stdlib-usage.html
Python Logging Module Handler https://google-cloud-python.readthedocs.io/en/0.32.0/logging/handlers.html
Recommended Posts