使用Google Stackdriver记录App Engine标准版python时出错 [英] Error using Google Stackdriver Logging in App Engine Standard python

本文介绍了使用Google Stackdriver记录App Engine标准版python时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My Stack:

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将日志记录导入为stack_logging $ b $ from google.cloud.logging.resource import资源
导入线程

class StackdriverLogging:
def __init __(self,resource = Resource(type ='project',labels = {'project_id':'project_id'}),project_id ='project_id'):

self.resource =资源
self.client = stack_logging.Client(project = project_id)
$ b $ def delete_logger(self,logger_name):
logger = self.client.logger(logger_name)
logger.delete )
$ b $ 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,I​​NFO,msg =world)

错误:
找到1个没有匹配响应的RPC请求



如果Google App Engine Standard(Python)任何获得此代码的方法:从b

  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日志模块直接绑定到Google Stackdriver Logging。
为此,我创建了一个log_helper模块:

  from google.cloud将日志记录导入为gc_logging 
导入日志
$ b logging_client = gc_logging.Client()
logging_client.setup_logging(logging.INFO)

从日志导入*


 <$>然后我将其导入到其他文件中,例如: c $ c> import log_helper as logging 

之后你可以像使用默认的python一样使用模块日志模块。



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

 导入log_helper作为日志记录

test_logger = logging.getLogger('test')
test_logger.setLevel(logging.INFO)
test_logger。 info('是这个记录器的名字')

输出:


INFO:test:是此记录器的名称



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.

解决方案

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

这篇关于使用Google Stackdriver记录App Engine标准版python时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆