重复的日志烧瓶-Google云日志记录

时间:2020-03-27 04:03:54

标签: python-3.x flask logging google-cloud-platform gunicorn

我正在使用GAE在Flask中开发一个Web应用程序。 我的问题是:每次我的应用程序尝试登录时,我都会在日志文件上获得多个条目: log viewer 。 我的dbconnection类仅导入我创建的默认记录器类,并在需要时调用 unexpected_error_log()进行写入。

我的记录器课程:

import logging
from google.cloud import logging as cloudlogging

class LoggerDB:
    def __init__(self):
        log_client = cloudlogging.Client()
        log_handler = log_client.get_default_handler()
        self.cloud_logger = logging.getLogger("cloudLogger")
        self.cloud_logger.setLevel(logging.INFO)
        self.cloud_logger.addHandler(log_handler)

    def unexpected_error_log(self, name, error="Unhandled Exception"):
        self.cloud_logger.error("Unexpected Error on %s: %s", name, error)

执行时的代码:

def insertVenda(self, venda):
    try:
        query = "xxxxx"
        self.cursor.execute(query)
        self.connection.commit()
        return "Success"
    except Exception as error:
        self.logger.unexpected_error_log(__name__, error)
        self.connection.rollback()
        return "Error"

我怀疑gunicorn / app日志正在复制我的日志,但是我不知道该如何处理。 有人有同样的问题吗?

1 个答案:

答案 0 :(得分:0)

此刻我正在为此而苦苦挣扎,如果您包含以下内容,我现在很怀疑:

# Imports Python standard library logging
import logging

import google.cloud.logging

# Instantiates a client
client = google.cloud.logging.Client()

# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.get_default_handler()
client.setup_logging()

我得到的日志还可以,但是有多个重复项。 如果我省略,我只会在堆栈驱动程序日志中获得单个stdout打印语句。