python-3.x - 重复的日志 Flask - Google Cloud Logging

我正在使用 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 logging 正在复制我的日志,但我不知道如何处理这种情况。 有人遇到过同样的问题吗?

最佳答案

目前我正在为此苦苦挣扎,目前我怀疑您是否包括这样的内容:

# 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()

我得到的日志正常,但有多个重复项。 如果我省略,我只会将单个标准输出打印语句发送到堆栈驱动程序日志。

https://stackoverflow.com/questions/60879847/

相关文章:

r - 为什么 clogit 和 bife 函数(均在 R 中)的结果不同?

asp.net - SignalR 协商阶段发生 CORS 错误

django - 无法在 nginx docker 容器上使用 http2

node.js - Nodejs 等待不等待

reactjs - 将 Ant 设计表导出为 pdf 格式

javascript - 如何访问 react-image-gallery 中的 slideToIn

swift - 分段选择器移除了可访问性

python - 无需安装即可导入 Python 库

react-native - 使用 Hermes 的 React native 应用程序崩溃并显示

angular - 类型 'arrayBuffer' 上不存在属性 'File'