我正在使用 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/