1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[GCP]Cloud FunctionsのログをCloud Logging(Stackdriver Logging)に出力する方法(Python)

Posted at

はじめに

Pythonで書いたCloud FunctionsのログをCloud Logging(Stackdriver Logging)に出力する方法を記載しています。

手順

以下のように書きます。
cloud_logger.setLevelで指定したレベル以上のものがCloud Loggingに記録されます。

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')

requirements.txt には以下2つを記載してください。(バージョン番号は省略しています)

google-cloud-logging
google-cloud-core

google-cloud-coreを書かない場合は以下のエラーがでました。

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?