【发布时间】:2020-04-22 00:04:00
【问题描述】:
我有这个类用于记录到 StackDriver。
from google.cloud.logging.handlers import CloudLoggingHandler
from google.oauth2 import service_account
class GoogleLogger(CloudLoggingHandler):
CREDS = google.cloud.logging.Client(
project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds"))
def __init__(self, client=CREDS):
super(GoogleLogger, self).__init__(client)
在谷歌云上运行时,可以无缝运行。但是,当在本地运行时,它会在 CREDS = google.cloud.logging.Client(project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds")) 处中断
我的整个代码都中断了。
问题:如果不在云端,有什么方法可以跳过实例化这个类?像一个条件类?
或
问题:有没有办法在本地进行这项工作?它应该工作。我给它信用和项目,根据StackDriver docs,如果我给它项目+信用,它应该在本地以及在 GCP 中工作?
当它中断时,这是回溯:
Traceback (most recent call last):
File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 27, in <module>
class GoogleLogger(CloudLoggingHandler):
File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 36, in GoogleLogger
project=PROJECT, credentials=service_account.Credentials.from_service_account_file(local_or_gcp()))
File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/client.py", line 123, in __init__
self._connection = Connection(self, client_info=client_info)
File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/_http.py", line 39, in __init__
super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given
【问题讨论】:
-
当它中断时你会遇到什么错误?
-
@MicahCarrick 编辑并添加回溯到问题。
-
您运行的是哪个版本的
google-cloud-core?pip show google-cloud-core -
Version: 0.24.1 -
@MicahCarrick 我会更新这个,然后回复你。我觉得您可能是正确的,因为我的计算引擎实例上安装了 1.1 版。
标签: python python-3.x logging google-cloud-platform stackdriver