【问题标题】:Error using Google Stackdriver Logging in App Engine Standard python在 App Engine Standard python 中使用 Google Stackdriver Logging 时出错
【发布时间】:2018-04-23 17:39:26
【问题描述】:

我的堆栈:
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") 

如果需要凭据,我喜欢使用项目服务帐户。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 几天前刚刚查看了一些 Python 日志记录问题,当时我注意到一个名为“app”的 Stackdriver 日志。不知道这个名字是从哪里来的,但它正在按照我的意愿进行记录,尊重日志级别和所有内容。在寻找这方面的文档时,我遇到了以下问题:googlecloudplatform.github.io/google-cloud-python/latest/…。我没有使用这个,但也许它会帮助你?

标签: python google-app-engine google-cloud-platform google-cloud-logging google-cloud-stackdriver


【解决方案1】:

我通常将 Python 日志记录模块直接绑定到 Google Stackdriver Logging。 为此,我创建了一个 log_helper 模块:

from google.cloud import logging as gc_logging
import logging

logging_client = gc_logging.Client()
logging_client.setup_logging(logging.INFO)

from logging import *

然后我将其导入到其他文件中:

import log_helper as logging

之后,您可以像使用默认 python 日志记录模块一样使用该模块。

要使用默认的 python 日志记录模块创建命名日志,请为不同的命名空间使用不同的记录器:

import log_helper as logging

test_logger = logging.getLogger('test')
test_logger.setLevel(logging.INFO)
test_logger.info('is the name of this logger')

输出:

INFO:test:是这个记录器的名字

【讨论】:

  • 这不是正确的答案!,这不能解决创建命名日志的问题
  • @BrianB 命名日志使用默认日志模块非常简单。您可以为不同的命名空间使用不同的记录器。
  • 如何使用 Google App Engine Standard (Python) 创建命名日志?
  • 我在答案中添加了关于如何创建命名日志的说明
猜你喜欢
  • 2018-10-12
  • 2018-05-27
  • 2018-12-11
  • 2019-12-28
  • 2018-05-08
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多