我正在我的烧瓶应用程序中进行性能测试。
ngnix -> gunicorn -> flask日志文件:
我在用ab测试工具。在我的开发环境(MacOS 10.10.5)中,我的请求被正确地处理并正确地记录在Flask文件中。
ab -c 10 -n 10 -A username:password https://1.1.1.1:8443/api/1.0/echo/当我转移到生产(Ubuntu)时,我无法看到所有10个请求都被记录在Flask文件中。我只在api日志文件中看到1。ab工具报告所有请求都被正确处理。
我在Ngnix,Gunicorn看到了10个请求,但没有在烧瓶中看到。
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 $TIMEOUTapi文件:
from application.app import api_app
if __name__ == "__main__":
api_app.run(debug=False, threaded=True)api_app瓶应用程序
# =========================================================
# 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.12logging_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]发布于 2016-05-18 18:26:31
当您声明log变量时,问题就会发生。gunicorn的多个线程调用多个getLogger实例,然后覆盖日志文件。如果您将Werkzeug服务器与threaded param app.run(threaded=True)一起使用,则不会发生这种情况。
如果您只需要记录请求,可以在gunicorn启动脚本上使用param --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在文档上看到更多关于loggin的信息。
警告:可能不安全
作为一个简单的解决方法,您可以将getLogger中的getLogger方法更改为附加到现有文件中,而不是覆盖该文件。这是不安全的,多个线程更改同一个文件会导致意外的结果。
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
显然是日志是线程安全的。,但我不确定这是否会扩展到枪手工人.
https://stackoverflow.com/questions/37294293
复制相似问题