Stackdriver python日志RPC错误

时间:2017-12-02 00:53:48

标签: python google-cloud-platform stackdriver google-cloud-stackdriver

我遇到了将错误记录到GCP StackDriver的RPC问题。以下是错误消息:

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.DEADLINE_EXCEEDED, Deadline Exceeded)>

以下是用于记录的python代码:

import logging
import logging.handlers
import os
import config
import google.cloud.logging as gcp_logging
from google.oauth2 import service_account

logger = logging.getLogger('my_logger')
## using Google Stackdriver logging
#client = gcp_logging.Client(project=config.project, credentials=config.credentials_gcp_ml)
#client = gcp_logging.Client.from_service_account_json('./cred.json')
cred = service_account.Credentials.from_service_account_file('./cred.json')
client = gcp_logging.Client(project = config.project, credentials=cred)
hdlr = client.get_default_handler()
logger = logging.getLogger('cloudLogger')

formatter = logging.Formatter('%(asctime)s  %(levelname)s   %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

我在连接到我的GCP帐户的本地计算机上运行此代码。

google-auth 1.2.0; google-cloud-logging 1.4.0

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码(稍加修改以使其适应我的项目,并添加最后一行以便将日志输出到Stackdriver Logging console)并且它对我有效。

在这里,我分享了我使用过的代码:

import logging
import logging.handlers
import os
import google.cloud.logging as gcp_logging
from google.oauth2 import service_account

logger = logging.getLogger('my_logger')
cred = service_account.Credentials.from_service_account_file('./private-key.json')
client = gcp_logging.Client(project = "<YOUR_PROJECT_ID>", credentials=cred)
hdlr = client.get_default_handler()
logger = logging.getLogger('cloudLogger')

formatter = logging.Formatter('%(asctime)s  %(levelname)s   %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('This is a Logging Test.')

在本地计算机上运行命令python test.py后,这是我获得的结果:

Program shutting down, attempting to send 1 queued log entries to Stackdriver Logging...
Waiting up to 5 seconds.
Sent all pending logs.

然后,如果我移动到之前提供的控制台链接并使用全局资源类型(resource.type="global")进行过滤,这就是我所看到的:

enter image description here

正如@ A.Queue所述,我也在运行google-auth版本1.3.0google-cloud-logging 1.4.0,因此您应该尝试升级google-auth,命令:

pip install --upgrade google-auth

如果同样的错误不断出现,请尝试并回复我们。