d函数中的stackdriver上记录重复的日志条目google

d函数中的stackdriver上记录重复的日志条目google

本文介绍了使用python在Google Cloud函数中的stackdriver上记录重复的日志条目google cloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日志记录处理程序附加到我的云函数中的Python根记录器中.我正在使用以下代码:

I am trying to attach a logging handler to the Python root logger in my cloud function. I am using below code :

    import logging
    import os
    import google.cloud.logging
    from google.cloud.logging.handlers import CloudLoggingHandler, setup_logging

    logLabels = {'ServerName' : os.uname().nodename}
    logging_client = google.cloud.logging.Client()
    handler = CloudLoggingHandler(logging_client,name='my-test-cf',labels=logLabels)
    logging.getLogger().setLevel(logging.INFO)
    setup_logging(handler, excluded_loggers=('google.cloud', 'google.auth', 'google_auth_httplib2'))
    logging.info(f"Starting Execution of My Test CF")

结果是重复的日志条目,一个带有信息,另一个带有错误

Result is duplicate log entries one with info and other with error

  1. info->开始执行我的测试CF
  2. 错误->开始执行我的测试CF

任何建议都会有很大帮助.谢谢你.

Any suggestions would be of great help. Thank you.

推荐答案

首先,您需要设置日志记录,以将Cloud Logging处理程序附加到Python Root logger,以将所有日志查询发送到Cloud Logging.

First you need to setup the logging to sent all log intries to Cloud Logging by attaching the Cloud Logging handler to the Python Root logger.

import google.cloud.logging
client = google.cloud.logging.Client()
client.get_default_handler()
client.setup_logging()

一旦附加了处理程序,应用程序中发出的所有日志(默认为INFO)都将发送到Logging.

Once the handler is attached, any logs (by default INFO) which are emitted in you application will be sent to Logging.

import logging
logging.info(f"Starting Execution of My Test CF")

这篇关于使用python在Google Cloud函数中的stackdriver上记录重复的日志条目google cloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:19