我的堆栈:
Google App Engine 标准
Python (2.7)
目标:
在 Google Stackdriver Logging 中创建命名日志,https: //console.cloud.google.com/logs/viewer
文档 - Stackdriver 日志记录: https ://google-cloud-python.readthedocs.io/en/latest/logging/usage.html
代码:
from google.cloud import logging as stack_logging
from google.cloud.logging.resource import Resource
import threading
class StackdriverLogging:
def __init__(self, resource=Resource(type='project', labels={'project_id': 'project_id'}), project_id='project_id'):
self.resource = resource
self.client = stack_logging.Client(project=project_id)
def delete_logger(self, logger_name):
logger = self.client.logger(logger_name)
logger.delete()
def async_log(self, logger_name, sev, msg):
t = threading.Thread(target=self.log, args=(logger_name, sev, msg,))
t.start()
def log(self, logger_name, sev, msg):
logger = self.client.logger(logger_name)
if isinstance(msg, str):
logger.log_text(msg, severity=sev, resource=self.resource)
elif isinstance(msg, dict):
logger.log_struct(msg, severity=sev, resource=self.resource)
class hLog(webapp2.RequestHandler):
def get(self):
stackdriver_logger = StackdriverLogging()
stackdriver_logger.async_log("my_new_log", "WARNING", msg="Hello")
stackdriver_logger.async_log("my_new_log", "INFO", msg="world")
错误: 找到 1 个没有匹配响应的 RPC 请求
如果这在 Google App Engine 标准 (Python) 中是不可能的,那么可以通过任何方式使此代码正常工作:
from google.cloud import logging
client = logging.Client()
# client = logging.Client.from_service_account_json('credentials.json')
logger = client.logger("my_new_log")
logger.log_text("hello world")
如果需要凭据,我喜欢使用项目服务帐户。
任何帮助,将不胜感激。谢谢你。