【发布时间】:2020-10-10 02:00:16
【问题描述】:
我有一个使用 Docker compose 部署的 Flask 应用程序,附带一个小型数据库和一个 uWsgi 前端。我想将烧瓶应用程序的某些输出写入特定的日志文件。这个想法是,在午夜,我希望日志文件轮换到第二天,同时保留前 X 天的备份。我在应用首次启动时配置了日志文件,如下所示:
handler = TimedCompressedRotatingFileHandler('/home/root/custom_logs/no_RS_queries_planner_api.log',
when="midnight", encoding='utf8', backupCount=14)
logger = logging.getLogger('query_logger')
logger.addHandler(handler)
handler.suffix = "%Y-%m-%d"
# the all-important app variable:
application = Flask(__name__)
#sample log
logger.warning(value1,value2,value3)
当我在我的机器上本地测试 Flask 应用程序时,这有效,因为运行它只使用一个进程。但是,如果我尝试使用定义了多个工作人员的 uWsgi 来部署它.....:
; app.ini
[uwsgi]
protocol = uwsgi
chdir=/app/app/
; This is the name of our Python file
; minus the file extension
module = main
; This is the name of the variable
; in our script that will be called
callable = application
master = true;
; Set uWSGI to start up x workers
processes = 9
; We use the port 5000 which we will
; then expose on our Dockerfile
socket = 0.0.0.0:5000
vacuum = trued
die-on-term = true
所有工作进程都会在午夜对同一个文件进行翻转。基本上多次覆盖它并导致我丢失日志文件。这似乎仍然是此类部署中的当前问题,并且尝试解决起来非常烦人。有没有什么简单的方法来处理这类事情,而不必大规模地重新构建我的应用程序?
作为参考,TimedCompressedRotatingFileHandler 是从here 中提取的现有日志轮换类的修改。我尝试了一些厚颜无耻的方法,例如尝试获取 uWsgi 工作人员 ID 并将其作为附加后缀附加到日志文件中,但这似乎不起作用,只输出 0。我可以尝试记录到数据库并在午夜按计划输出,但我更愿意先尝试记录方法。
谁能帮我找到解决问题的快速方法?
【问题讨论】:
标签: python logging concurrency uwsgi