使用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 中看到日志输出,只能看到打印语句。这是一些不使用 google.cloud.logging
的示例代码,这确实成功地导致日志输出出现在我的终端中:
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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-20220306211233889260
信息:根:job_id=qwerty-20220306211233889260
信息:根:total_bytes_billed=31457280
如您所见,对 print(...) 的调用已输出到我的终端,并且对logging.info(...) 的调用已输出到我的终端> 也有。
如果我将代码更改为也使用 google.cloud.logging
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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}"
那么我在终端中看不到日志输出:
开始作业:provider-egress-hastings-20220306211718088936
有没有一种方法可以在使用函数框架本地运行和使用 google.cloud.logging
时将日志输出重定向到我的终端,但在函数运行时不影响日志记录是否作为 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 some sample code that does NOT use google.cloud.logging
, this DOES successfully cause logging output to appear in my terminal:
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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}"
Here is the output
Started job: qwerty-20220306211233889260
INFO:root:job_id=qwerty-20220306211233889260
INFO:root:total_bytes_billed=31457280
As you can see the call to print(...)
has outputted to my terminal and the call to logging.info(...)
has as well.
If I change the code to also use google.cloud.logging
from flask import Request
from google.cloud import bigquery
from datetime import datetime
import google.cloud.logging
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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}"
Then I don't see the logging output in the terminal:
Started job: provider-egress-hastings-20220306211718088936
Is there a way to redirect logging output to my terminal when running locally using functions-framework and when using google.cloud.logging
but not affect logging when the function is running as an actual cloud function in GCP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在本地测试中使用环境变量来配置控制台处理程序:
i'm using an environment variable in local testing to configure a console handler:
就像@hincha 发布的那样,您可以使用下面的示例:
之后我可以在终端上看到日志
Like posted by @hincha, you can use the example bellow:
After that I could see the logs on terminal