WSGI未将Flask @ app.route装饰器的默认日志记录行发送到Apache2 error.out

时间:2018-08-13 13:21:36

标签: logging flask apache2 mod-wsgi 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运行时,代码中的日志行将记录到apache2 error.out中,但从127.0.0.1开始的行是在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     

1 个答案:

答案 0 :(得分:1)

请求日志是开发服务器的一部分,在生产设置(或任何其他不使用开发服务器的设置)中,由Web服务器记录请求。

如果您真的想自己记录这些日志,则必须自己实施,例如当Flask破坏应用程序上下文时,请使用钩子。