谷歌云函数 Python 日志记录问题 [英] Google Cloud Functions Python Logging issue

查看:23
本文介绍了谷歌云函数 Python 日志记录问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么说,但是我觉得有一些事情在我不知道的情况下被谷歌改变了.我曾经在日志记录仪表板中的 Google Cloud Console 中从我的 python Cloud Functions 获取我的日志.现在,它停止工作了.

于是去查了半天,就做了一个日志hello world python Cloud Function:

导入日志def cf_endpoint(req):logging.debug('日志调试')logging.info('日志信息')logging.warning('日志警告')logging.error('记录错误')logging.critical('日志关键')返回确定"

这是我的 main.py,我将它部署为带有 h​​ttp 触发器的 Cloud Function.

由于我有一个包含所有调试"级别日志的日志摄取排除过滤器,因此我在日志记录仪表板中没有看到任何内容.但是当我删除它时,我发现了这一点:

所以看起来像解析python内置日志记录到stackdriver的东西停止解析日志严重性参数!如果我看起来很愚蠢,我很抱歉,但这是我唯一能想到的:/

你们对此有任何解释或解决方案吗?我做错了吗?

预先感谢您的帮助.

解决方案

使用 Python 原生 日志记录 模块.

但是,您仍然可以使用Stackdriver Logging Client Libraries<创建具有特定严重性的日志/a>.检查 本文档 参考 Python 库,以及这个 一些用例示例.>

请注意,为了让日志位于正确的资源下,您必须手动配置它们,请参阅 此列表 用于支持的资源类型.同样,每种资源类型都有一些必需标签 需要出现在日志结构中.

例如,以下代码将在 Stackdriver Logging 中将日志写入 Cloud Function 资源,并具有 ERROR 严重性:

from google.cloud 导入日志从 google.cloud.logging.resource 导入资源log_client = logging.Client()# 这是日志的资源类型log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'# 在资源内部,嵌套特定于资源类型的必需标签res = Resource(type="cloud_function",标签={"function_name": "您的云功能名称","region": "您的功能位置"},)logger = log_client.logger(log_name.format("YOUR-PROJECT-ID"))logger.log_struct({"message": "要记录的消息字符串"}, 资源=res, 严重性='错误')return 'Wrote logs to {}.'.format(logger.name) # 返回云函数响应

注意YOUR-CLOUD-FUNCTION-NAMEYOUR-FUNCTION-LOCATIONYOUR-PROJECT-ID中的字符串需要特定于您的项目/资源.

I'm not sure how to say this but, I'm feeling like there is something under the hood that was changed by Google without me knowing about it. I used to get my logs from my python Cloud Functions in the Google Cloud Console within the logging dashboard. And now, it just stopped working.

So I went investigating for a long time, I just made a log hello world python Cloud Function:

import logging

def cf_endpoint(req):
    logging.debug('log debug')
    logging.info('log info')
    logging.warning('log warning')
    logging.error('log error')
    logging.critical('log critical')
    return 'ok'

So this is my main.py that I deploy as a Cloud Function with an http trigger.

Since I was having a log ingestion exclusion filter with all the "debug" level logs I wasn't seeing anything in the logging dashboard. But when I removed it I discovered this :

So it seems like something that was parsing the python built-in log records into stackdriver stopped parsing the log severity parameter! I'm sorry if I look stupid but that's the only thing I can think about :/

Do you guys have any explanations or solutions for this ? am I doing it the wrong way ?

Thank you in advance for your help.

解决方案

Stackdriver Logging severity filters are no longer supported when using the Python native logging module.

However, you can still create logs with certain severity by using the Stackdriver Logging Client Libraries. Check this documentation in reference to the Python libraries, and this one for some usage-case examples.

Notice that in order to let the logs be under the correct resource, you will have to manually configure them, see this list for the supported resource types. As well, each resource type has some required labels that need to be present in the log structure.

As an example, the following code will write a log to the Cloud Function resource, in Stackdriver Logging, with an ERROR severity:

from google.cloud import logging
from google.cloud.logging.resource import Resource

log_client = logging.Client()

# This is the resource type of the log
log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions' 

# Inside the resource, nest the required labels specific to the resource type
res = Resource(type="cloud_function", 
               labels={
                   "function_name": "YOUR-CLOUD-FUNCTION-NAME", 
                   "region": "YOUR-FUNCTION-LOCATION"
               },
              )
logger = log_client.logger(log_name.format("YOUR-PROJECT-ID"))
logger.log_struct(
 {"message": "message string to log"}, resource=res, severity='ERROR')

return 'Wrote logs to {}.'.format(logger.name) # Return cloud function response

Notice that the strings in YOUR-CLOUD-FUNCTION-NAME, YOUR-FUNCTION-LOCATION and YOUR-PROJECT-ID, need to be specific to your project/resource.

这篇关于谷歌云函数 Python 日志记录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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