WSGI 未将来自 Flask @app.route 装饰器的默认日志记录行发送到 Apache2 error.out
Default logging lines from Flask @app.route decorator not sent to Apache2 error.out by WSGI
我们有一个 flask
应用 运行 正在 WSGI
上使用 apache2
。
app.logger.<level>(message)
行在代码中工作正常。但是,@app.route
烧瓶装饰器生成的通用日志行不会出现。
当 flask 应用程序是 运行 手动时,它们 会 出现,而不是当 运行 通过 Apache2 WSGI 时。
如何让自动生成的日志行通过 WSGI
发送到 apache2 error.out 日志
即运行 手动
# flask run
--------------------------------------------------------------------------------
DEBUG in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:34]:
Using app defaults, please provide a valid config file
--------------------------------------------------------------------------------
* Serving Flask app "APPNAME.app"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
--------------------------------------------------------------------------------
ERROR in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:102]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
TEST IN welcome(route)
--------------------------------------------------------------------------------
WARNING in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:103]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
TEST IN welcome(route)
--------------------------------------------------------------------------------
INFO in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:104]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:105]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/selectize.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/material.min.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:46] "GET /static/css/style.css HTTP/1.1" 200 -
当运行通过WSGI时,代码中的日志行记录到apache2error.out
,但是以[=23=开头的行] 从 Flask 装饰器本身内部创建的 不会 记录到 apache2 error.out。
-------------------------------------------- ----------------------
=== 当 WSGI 运行 时未发送到 apache2 error.out 的行 ===
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/selectize.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/material.min.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:46] "GET /static/css/style.css HTTP/1.1" 200 -
=== WSGI.py 文件 ===
#!/usr/bin/env python
# coding=utf-8
import os
import sys
import logging
import site
# The path of the APPNAME project directory is the same as the directory of this file
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
logging.basicConfig(stream=sys.stderr)
level = logging.getLevelName('DEBUG')
logger= logging.getLogger(__file__)
logger.setLevel(level)
logger.debug("Logging enabled via \"logger=logging.getLogger('" + __file__ + "'\"")
site.addsitedir(PROJECT_DIR)
sys.path.append(PROJECT_DIR)
from APPNAME.app import app as application
=== APPNAME.py 文件 ===
from APPNAME.workers import filter_genes, run_analyse
this_dir = os.path.dirname(os.path.abspath(__file__))
app = flask.Flask('APPNAME',
template_folder=os.path.join(this_dir, 'templates'),
static_folder=os.path.join(this_dir, 'static'))
app.secret_key = os.urandom(24)
app.config.from_pyfile(os.path.join(this_dir, 'config.py'))
app.logger.addHandler(logging.getLogger('rq.worker'))
try:
user_config = app.config.from_pyfile(sys.argv[1])
except FileNotFoundError:
msg = 'Using app defaults, please provide a valid config file'
app.logger.debug(msg)
except IndexError:
pass
Genome.init(app.config['DATA_PATH'])
Regulator.init(app.config['DATA_PATH'])
conn = Redis(charset="utf-8", decode_responses=True)
# assert redis is running
conn.ping()
@app.route('/')
def welcome():
app.logger.error("TEST IN welcome(route)") #3333
app.logger.warning("TEST IN welcome(route)") #3333
app.logger.info("TEST IN welcome(route)") #3333
app.logger.debug("TEST IN welcome(route)") #3333
return render_template('welcome.html')
if __name__ == "__main__":
print(app.config)
app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT'])
=== wsgi.py 配置文件 ===
# !/usr/bin/env python3
# coding=utf-8
DATA_PATH='/var/www/APP/data/'
SESSION_TTL=3600
RESULT_TTL=86400
REGULATORS_TTL=3600
MAX_RESULTS=100
SESSION_STORE="/tmp/APPNAME-{unique_id}"
PORT=5000
HOST='0.0.0.0'
DEBUG=True
TEMPLATES_AUTO_RELOAD = True
请求日志是开发服务器的一部分,在生产设置(或任何其他不使用开发服务器的设置)中,由网络服务器记录请求。
如果你真的想自己记录这些,你必须自己实现,例如当 Flask 拆除应用程序上下文时使用钩子。
我们有一个 flask
应用 运行 正在 WSGI
上使用 apache2
。
app.logger.<level>(message)
行在代码中工作正常。但是,@app.route
烧瓶装饰器生成的通用日志行不会出现。
当 flask 应用程序是 运行 手动时,它们 会 出现,而不是当 运行 通过 Apache2 WSGI 时。
如何让自动生成的日志行通过 WSGI
发送到 apache2 error.out 日志即运行 手动
# flask run
--------------------------------------------------------------------------------
DEBUG in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:34]:
Using app defaults, please provide a valid config file
--------------------------------------------------------------------------------
* Serving Flask app "APPNAME.app"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
--------------------------------------------------------------------------------
ERROR in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:102]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
TEST IN welcome(route)
--------------------------------------------------------------------------------
WARNING in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:103]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
TEST IN welcome(route)
--------------------------------------------------------------------------------
INFO in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:104]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG in app [/var/www/APP/lib/python3.4/site-packages/APPNAME/app.py:105]:
TEST IN welcome(route)
--------------------------------------------------------------------------------
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/selectize.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/material.min.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:46] "GET /static/css/style.css HTTP/1.1" 200 -
当运行通过WSGI时,代码中的日志行记录到apache2error.out
,但是以[=23=开头的行] 从 Flask 装饰器本身内部创建的 不会 记录到 apache2 error.out。
-------------------------------------------- ----------------------
=== 当 WSGI 运行 时未发送到 apache2 error.out 的行 ===
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/selectize.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:45] "GET /static/css/material.min.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Aug/2018 14:44:46] "GET /static/css/style.css HTTP/1.1" 200 -
=== WSGI.py 文件 ===
#!/usr/bin/env python
# coding=utf-8
import os
import sys
import logging
import site
# The path of the APPNAME project directory is the same as the directory of this file
PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))
logging.basicConfig(stream=sys.stderr)
level = logging.getLevelName('DEBUG')
logger= logging.getLogger(__file__)
logger.setLevel(level)
logger.debug("Logging enabled via \"logger=logging.getLogger('" + __file__ + "'\"")
site.addsitedir(PROJECT_DIR)
sys.path.append(PROJECT_DIR)
from APPNAME.app import app as application
=== APPNAME.py 文件 ===
from APPNAME.workers import filter_genes, run_analyse
this_dir = os.path.dirname(os.path.abspath(__file__))
app = flask.Flask('APPNAME',
template_folder=os.path.join(this_dir, 'templates'),
static_folder=os.path.join(this_dir, 'static'))
app.secret_key = os.urandom(24)
app.config.from_pyfile(os.path.join(this_dir, 'config.py'))
app.logger.addHandler(logging.getLogger('rq.worker'))
try:
user_config = app.config.from_pyfile(sys.argv[1])
except FileNotFoundError:
msg = 'Using app defaults, please provide a valid config file'
app.logger.debug(msg)
except IndexError:
pass
Genome.init(app.config['DATA_PATH'])
Regulator.init(app.config['DATA_PATH'])
conn = Redis(charset="utf-8", decode_responses=True)
# assert redis is running
conn.ping()
@app.route('/')
def welcome():
app.logger.error("TEST IN welcome(route)") #3333
app.logger.warning("TEST IN welcome(route)") #3333
app.logger.info("TEST IN welcome(route)") #3333
app.logger.debug("TEST IN welcome(route)") #3333
return render_template('welcome.html')
if __name__ == "__main__":
print(app.config)
app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT'])
=== wsgi.py 配置文件 ===
# !/usr/bin/env python3
# coding=utf-8
DATA_PATH='/var/www/APP/data/'
SESSION_TTL=3600
RESULT_TTL=86400
REGULATORS_TTL=3600
MAX_RESULTS=100
SESSION_STORE="/tmp/APPNAME-{unique_id}"
PORT=5000
HOST='0.0.0.0'
DEBUG=True
TEMPLATES_AUTO_RELOAD = True
请求日志是开发服务器的一部分,在生产设置(或任何其他不使用开发服务器的设置)中,由网络服务器记录请求。
如果你真的想自己记录这些,你必须自己实现,例如当 Flask 拆除应用程序上下文时使用钩子。