Error using Google Stackdriver Logging in App Engine Standard python

风格不统一 提交于 2019-12-04 11:00:58

问题


My Stack:
Google App Engine Standard
Python (2.7)

Goal:
To create named logs in Google Stackdriver Logging, https://console.cloud.google.com/logs/viewer

Docs - Stackdriver Logging: https://google-cloud-python.readthedocs.io/en/latest/logging/usage.html

Code:

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

ERROR: Found 1 RPC request(s) without matching response

If this is not possible in Google App Engine Standard (Python) any way to get this code to work:

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

If credentials are required, I like to use the project service account.

Any help would be appreciated. Thank you.


回答1:


I usually tie the Python logging module directly into Google Stackdriver Logging. To do this, I create a log_helper module:

from google.cloud import logging as gc_logging
import logging

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

from logging import *

Which I then import into other files like this:

import log_helper as logging

After which you can use the module like you would the default python logging module.

To create named logs with the default python logging module, use different loggers for different namespaces:

import log_helper as logging

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

Output:

INFO:test:is the name of this logger



来源:https://stackoverflow.com/questions/47223678/error-using-google-stackdriver-logging-in-app-engine-standard-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!