多个请求Flask,Gunicorn,Nginx日志记录无法正常工作

时间:2016-05-18 08:35:39

标签: python logging flask gunicorn flask-restful

我在Flask应用程序中进行性能测试。

  ngnix -> gunicorn -> flask

日志文件:

  • access.log 确定
  • gunicorn.log 确定
  • api.log 只显示1个请求

我正在使用ab测试工具。我的请求在Dev环境(MacOS 10.10.5)中的Flask日志文件中正确处理并正确记录。

ab -c 10 -n 10 -A username:password https://1.1.1.1:8443/api/1.0/echo/

当我转向生产时,(Ubuntu)我看不到Flask日志文件中记录的所有10个请求。我只在api日志文件中看到1。 ab 工具报告所有请求都得到了妥善处理。

我确实在Ngnix,Gunicorn中看到了10个请求,但在Flask中没有。

gunicorn 启动脚本(Supervisord)

GUNICORN_LOGFILE=/usr/local/src/imbue/application/imbue/log/gunicorn.log
NUM_WORKERS=8
TIMEOUT=60
WORKER_CONNECTIONS=2000
BACKLOG=1000
exec gunicorn api:api_app --bind 0.0.0.0:8081 --log-level=DEBUG --log-file=$GUNICORN_LOGFILE --workers $NUM_WORKERS --worker-connections=$WORKER_CONNECTIONS --backlog=$BACKLOG --timeout $TIMEOUT

api 档案:

from application.app import api_app

if __name__ == "__main__":
    api_app.run(debug=False, threaded=True)

api_app Flask应用

# =========================================================
# API Logs
# =========================================================

log = logging_conf.LoggerManager().getLogger("___app___", logging_file=settings.api_logfile)

# =========================================================
# Flask Application imports and database
# =========================================================
from flask import Flask
from flask.ext import restful
from werkzeug.contrib.fixers import ProxyFix

# =========================================================
# Flask Main Application
# =========================================================

api_app = Flask(__name__)  # Flask Application
api_app.config.from_pyfile("../../../conf/settings.py")  # Flask configuration

imbue_api = restful.Api(api_app)  # Define API
db = Model.db.init_app(api_app)  # Initialize database

# =========================================================
# API Definition
# =========================================================

imbue_api.add_resource(ApiBase, settings.BASE_API_URL)
imbue_api.add_resource(Echo, '/api/1.0/echo/')
# =========================================================
# API Proxy WSGi for gunicorn
# =========================================================

api_app.wsgi_app = ProxyFix(api_app.wsgi_app)
log.info('Initializing API >>>')

api_main

import logging

# =========================================================
# IMBUE imports
# =========================================================

from flask import current_app
from flask import jsonify, request, Response, url_for
from flask.ext import restful
from flask.ext.restful import Resource

# =========================================================
# API Controller
# =========================================================

api = restful.Api

# =========================================================
# API Logs
# =========================================================

log = logging_conf.LoggerManager().getLogger("___app___", logging_file=settings.api_logfile)

# =========================================================
# API Version information
# =========================================================

class Echo(Resource):
    @authenticator.requires_auth
    def get(self):
        """

        :return:
        """
        try:
            # =========================================================
            # GET API
            # =========================================================
            log.info(request.remote_addr + ' ' + request.__repr__())
            if request.headers['Content-Type'] == 'application/json':
                # =========================================================
                # Send API version information
                # =========================================================
                log.info('api() | GET | Version' + settings.api_version)
                response = json.dumps('version: ' + settings.api_version)
                resp = Response(response, status=200, mimetype='application/json')
                return resp

版本

gunicorn==19.3.0
Flask==0.10.1
Flask-HTTPAuth==2.7.0
Flask-Limiter==0.9.3
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-Migrate==1.6.0
Flask-OAuthlib==0.9.1
Flask-Principal==0.4.0
Flask-RateLimiter==0.2.0
Flask-RESTful==0.3.5
Flask-Restless==0.17.0
Flask-Script==2.0.5
Flask-Security==1.7.5
Flask-SQLAlchemy==2.1
Flask-WTF==0.12

logging_conf

class Singleton(type):
    """

    """
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances.keys():
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]


class LoggerManager(object):
    """

    """
    __metaclass__ = Singleton

    _loggers = {}

    def __init__(self, *args, **kwargs):
        pass

    @staticmethod
    def getLogger(name='___app___', logging_file='imbued.log', **kwargs):
        """

        :param name:
        :param logging_file:
        :param kwargs:
        :return:
        """
        # Define timezone
        logging.basicConfig(filename=logging_file,
                            filemode='w+',
                            level=logging.INFO,
                            format='%(asctime)s.%(msecs).03d %(levelname)s %(message)s',
                            datefmt='%m/%d/%Y %H:%M:%S')

        if not name:
            return logging.getLogger()
        elif name not in LoggerManager._loggers.keys():
            LoggerManager._loggers[name] = logging.getLogger(str(name))
        return LoggerManager._loggers[name]

1 个答案:

答案 0 :(得分:1)

当您声明log变量时,会出现问题。 gunicorn的多个线程调用多个getLogger实例,然后覆盖您的日志文件。当您将Werkzeug服务器与threaded param app.run(threaded=True)一起使用时,情况就不会发生。

如果您只需要记录请求,可以在gunicorn启动脚本上使用参数--access-logfile REQUEST_LOG_FILE

GUNICORN_LOGFILE=/usr/local/src/imbue/application/imbue/log/gunicorn.log
NUM_WORKERS=8
TIMEOUT=60
WORKER_CONNECTIONS=2000
BACKLOG=1000
REQUEST_LOG_FILE=./request-log-file.log
exec gunicorn api:api_app --bind 0.0.0.0:8081 --log-level=DEBUG --log-file=$GUNICORN_LOGFILE --workers $NUM_WORKERS --worker-connections=$WORKER_CONNECTIONS --backlog=$BACKLOG --timeout $TIMEOUT --access-logfile REQUEST_LOG_FILE

documentation上查看有关loggin的更多信息。

警告:可能不安全

作为一种简单的解决方法,您可以将getLogger中的basicConfig方法更改为,而不是覆盖该文件,附加到现有文件。这不安全,多个线程更改同一文件可能会导致意外结果。

logging.basicConfig(filename=logging_file,
                        filemode='w+', # Open, read and truncate the file.
                        level=logging.INFO,
                        format='%(asctime)s.%(msecs).03d %(levelname)s %(message)s',
                        datefmt='%m/%d/%Y %H:%M:%S')

logging.basicConfig(filename=logging_file,
                        filemode='a+', # Open, read and append to a file.
                        level=logging.INFO,
                        format='%(asctime)s.%(msecs).03d %(levelname)s %(message)s',
                        datefmt='%m/%d/%Y %H:%M:%S')

编辑1

显然logging is thread safe但我不确定是否会延长枪支工人......