代码之家  ›  专栏  ›  技术社区  ›  RightmireM

来自Flask@app.route decorator的默认日志记录行未被WSGI发送到Apache2 error.out

  •  0
  • RightmireM  · 技术社区  · 6 年前

    flask 应用程序运行于 WSGI 使用 apache2 .

    app.logger.<level>(message) @app.route 烧瓶装潢师不出现。

    他们 手动运行flask应用程序时出现,而不是通过Apache2 WSGI运行时出现。

    如何让WSGI将自动生成的日志行发送到apache2 error.out日志

    一、 e.手动运行

    # 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 -
    

    ,代码中的日志行记录到apache2 error.out ,但这些行以 127.0.0.1 不是 正在记录到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
    

    = 应用程序名.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'])   
    

    = 配置文件===

    # !/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 回复  |  直到 6 年前
        1
  •  1
  •   ThiefMaster    6 年前

    请求日志是dev服务器的一部分,在生产安装程序(或不使用dev服务器的任何其他安装程序)中,由web服务器记录请求。

    如果你真的想自己记录这些内容,你必须自己实现,例如当flask删除应用程序上下文时使用钩子。