在完成wsgi文件之前启动uwsgi写日志

时间:2017-07-31 22:24:55

标签: python logging uwsgi

在wsgi文件初始化完成之前,是否可以写入uwsgi日志?

我的应用程序在wsgi文件中有一个长时间运行的初始化,并且来自该初始化的日志会延迟到完成,这会使调试变得复杂(如果存在无限循环,也会导致零日志)。

例如,我使用此rm *.log; uwsgi --ini config.ini运行config.ini

[uwsgi]
http=:9090
logger=file:uwsgi.log
hook-master-start=exec:touch hook_started.log
wsgi-file=foobar.py

这是foobar.py

import logging
import os.path

logging.warn("logging entry")
with open('manual_init.log', 'w') as init_log_file:
    init_log_file.write("init file entry\n")

for expected_file_path in ('manual_init.log', 'hook_started.log', 'uwsgi.log'):
    logging.warn("%s file exists during init? %s" % (expected_file_path, os.path.exists(expected_file_path)))

def application(env, start_response):
    for expected_file_path in ('manual_init.log', 'hook_started.log', 'uwsgi.log'):
        logging.warn("%s file exists during app call? %s" % (expected_file_path, os.path.exists(expected_file_path)))

    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

uwsgi.log包含以下条目:

WARNING:root:logging entry
WARNING:root:manual_init.log file exists during init? True
WARNING:root:hook_started.log file exists during init? False
WARNING:root:uwsgi.log file exists during init? False

curl http://localhost:9090/之后,我获得了这些额外的uwsgi.log条目:

WARNING:root:manual_init.log file exists during app call? True
WARNING:root:hook_started.log file exists during app call? True
WARNING:root:uwsgi.log file exists during app call? True

0 个答案:

没有答案