Python Google Cloud Function日志记录的严重性和重复项 [英] Python Google Cloud Function Logging Severity and Duplicates
问题描述
我正在研究一个在Python运行时中运行的简单Google Cloud Function,我希望从Python程序到Stackdriver Logging进行一些简单的记录.
I'm working on a simple Google Cloud Function that runs in the Python runtime and I want some simple logging from the Python program to Stackdriver Logging.
根据Google的入门指南,这应该很简单 https://cloud.google.com/logging/docs/setup/python
Based on Google's startup guide, this should be straighforward https://cloud.google.com/logging/docs/setup/python
我设置了一个简单的测试功能来隔离我看到的日志记录问题
I set up a simple test function to isolate the logging issues I'm seeing
import google.cloud.logging
def logging_test_background(data, context):
logging_client = google.cloud.logging.Client()
logging_client.setup_logging()
logging.info("This should be info")
logging.debug("This should be debug")
logging.warning("This should be warning")
logging.error("This should be error")
在Stackdriver Logging中,一切变得混乱. 1.信息被复制为错误 2.调试未通过 3.警告重复,但两者均为错误记录级别 4.错误重复
In Stackdriver Logging everything gets jumbled. 1. Info is duplicated as an Error 2. Debug doesn't come through 3. Warning is duplicated, but both are error logging level 4. Error is duplicated
配置中是否缺少某些内容?
Is there something I'm missing in the configuration?
推荐答案
您需要创建自己的记录器,并向其中添加google-cloud-logging
默认处理程序:
You need to create your own logger and add the google-cloud-logging
default handler to it:
import logging
from flask import Flask
from google.cloud import logging as cloudlogging
log_client = cloudlogging.Client()
log_handler = log_client.get_default_handler()
cloud_logger = logging.getLogger("cloudLogger")
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(log_handler)
def logging_test_background(data, context):
cloud_logger.info("info")
cloud_logger.warning("warn")
cloud_logger.error("error")
return 'OK'
产生:
这篇关于Python Google Cloud Function日志记录的严重性和重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!