使用functions-framework和Google Cloud Logging Python v3.0.0时可以查看日志消息吗
我正在编写一个新的云函数,并使用新的 Google Cloud Logging 库,如 https://cloud.google.com/blog/products/devops-sre/google-cloud-logging-python-client-library-v3-0-0-release。
我还使用函数框架在本地调试我的代码,然后将其推送到 GCP。 使用 Python 设置和调用 Cloud Functions 在这里特别有用。
我遇到的问题是,当同时使用这两个东西时,我无法在 IDE 中看到日志输出,只能看到打印语句。这是我的代码示例:
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
log_client = google.cloud.logging.Client()
log_client.setup_logging()
def main(request) -> str:
#
# do stuff to setup a bigquery job
#
bq_client = bigquery.Client()
job_config = bigquery.QueryJobConfig(labels={"key": "value"})
nowstr = datetime.now().strftime("%Y%m%d%H%M%S%f")
job_id = f"qwerty-{nowstr}"
query_job = bq_client.query(
query=export_script, job_config=job_config, job_id=job_id
)
print("Started job: {}".format(query_job.job_id))
query_job.result() # Waits for job to complete.
logging.info(f"job_id={query_job.job_id}")
logging.info(f"total_bytes_billed={query_job.total_bytes_billed}")
return f"{query_job.job_id} {query_job.state} {query_job.error_result}"
但是,当我使用云函数运行该函数时,我在终端中看到的唯一输出是
开始工作:qwerty-20220306181905424093
如您所见,对 print(...) 的调用已输出到我的终端,但对logging.info(...) 的调用已输出到我的终端没有。有没有办法在使用函数框架本地运行时将日志记录输出重定向到我的终端,但在函数作为 GCP 中的实际云函数运行时不影响日志记录?
I am writing a new cloud function and am using the new Google Cloud Logging library as announced at https://cloud.google.com/blog/products/devops-sre/google-cloud-logging-python-client-library-v3-0-0-release.
I am also using functions-framework to debug my code locally before pushing it to GCP. Setup and Invoke Cloud Functions using Python has been particularly useful here.
The problem I have is that when using these two things together I cannot see logging output in my IDE, I can only see print statements. Here's a sample of my code:
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
log_client = google.cloud.logging.Client()
log_client.setup_logging()
def main(request) -> str:
#
# do stuff to setup a bigquery job
#
bq_client = bigquery.Client()
job_config = bigquery.QueryJobConfig(labels={"key": "value"})
nowstr = datetime.now().strftime("%Y%m%d%H%M%S%f")
job_id = f"qwerty-{nowstr}"
query_job = bq_client.query(
query=export_script, job_config=job_config, job_id=job_id
)
print("Started job: {}".format(query_job.job_id))
query_job.result() # Waits for job to complete.
logging.info(f"job_id={query_job.job_id}")
logging.info(f"total_bytes_billed={query_job.total_bytes_billed}")
return f"{query_job.job_id} {query_job.state} {query_job.error_result}"
However when I run the function using cloud functions the only output I see is in my terminal is
Started job: qwerty-20220306181905424093
As you can see the call to print(...)
has outputted to my terminal but the call to logging.info(...)
has not. Is there a way to redirect logging output to my terminal when running locally using functions-framework but not affect logging when the function is running as an actual cloud function in GCP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 @cryptofool 的建议,我发现我需要更改默认日志记录级别才能使输出出现在终端中。
但是,使用
google.cloud.logging
时,我仍然无法在终端中输出任何内容我想我会开始另一个关于这个的话题。
Thanks to the advice from @cryptofool I figured out that I needed to change the default logging level to get output to appear in the terminal.
However, I still can't any output in the terminal when using
google.cloud.logging
I think I'll start another thread about this.