多个请求Flask、Gunicorn、Nginx日志记录不起作用

2024-04-20 01:34:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在我的烧瓶应用程序中做性能测试。在

  ngnix -> gunicorn -> flask

日志文件:

  • 在访问.log好的
  • 在古尼康.log好的
  • 在api.日志只显示1个请求

我在用ab测试工具。在我的开发环境(MacOS10.10.5)中,我的请求被正确地处理并正确地记录在Flask日志文件中。在

^{pr2}$

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

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

gunicorn开始脚本(主管)

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应用程序烧瓶应用程序

# =========================================================
# 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主页

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

记录配置

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]

Tags: namefromimportlogapiappflasksettings
1条回答
网友
1楼 · 发布于 2024-04-20 01:34:31

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

如果只需要记录请求,可以在gunicorn start脚本上使用参数 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方法更改为附加到现有文件,而不是覆盖该文件。这是不安全的,多个线程更改同一个文件可能会导致意外结果。在

^{pr2}$

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但我不确定这是否会扩展到gunicorn工人。。。在

相关问题 更多 >