我正在使用python准备一些日志记录,但是每当我运行我的代码时,它都会生成日志,但是在stackdriver console上显示两次(一个作为信息,一个作为错误)。任何人都有如何处理此问题的想法。
我的代码:
import logging
from google.cloud import logging as gcp_logging
log_client = gcp_logging.Client()
log_client.setup_logging()
# here executing some bigquery operations
logging.info("Query result loaded into temporary table: {}".format(temporary_table))
# here executing some bigquery operations
logging.error("Query executed with empty result set.")
当我运行以上代码时,它在堆栈驱动程序上两次显示在日志上方。
Info:2019-10-17T11:54:02.504Z cf-mycloudfunctionname查询结果已加载到临时表mytable中
错误:2019-10-17T11:54:02.505Z cf-mycloudfunctionname查询结果已加载到临时表:mytable中
我看到的是你们两个(错误,信息)都被识别为文本平面,因此它发送的信息与stderr和stdout的信息相同,这就是为什么您会收到两条相同的消息。
您需要做的是纠正这两个登录到structured JSON的短语,此错误将被堆栈驱动程序识别为一个具有正确有效负载的实体。
另外,您可以配置堆栈驱动器代理以根据需要发送日志来发送日志,请看this document。
也取决于您尝试从何处检索此日志GCE,GKE,BigQ。在某些情况下,最好直接更改fluentd的结构,而不是使用stackdriver agent。