Google Cloud Functions Python 日志记录问题 [英] Google Cloud Functions Python Logging issue

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

问题描述

我不知道该怎么说,但我感觉 Google 在我不知情的情况下更改了某些内容.我曾经在日志仪表板的 Google Cloud Console 中从我的 python Cloud Functions 获取日志.而现在,它刚刚停止工作.

于是调查了半天,刚刚做了一个log hello world python Cloud Function:

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

这是我的 main.py,我将其部署为带有 h​​ttp 触发器的云函数.

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

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

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

提前感谢您的帮助.

解决方案

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

但是,您仍然可以使用 Stackdriver Logging 客户端库.检查 本文档 以参考 Python 库,以及这个一些用例示例.p>

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

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

来自 google.cloud 的导入日志从 google.cloud.logging.resource 导入资源log_client = logging.Client()# 这是日志的资源类型log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'# 在资源内部,嵌套特定于资源类型的所需标签res = 资源(type="cloud_function",标签={"function_name": "你的云函数名",区域":您的功能位置"},)logger = log_client.logger(log_name.format("YOUR-PROJECT-ID"))logger.log_struct({消息":要记录的消息字符串"},资源=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.

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

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