2017-08-30 44 views
0

我已經部署在ngnix/uwsgi燒瓶中的應用程序和我收到一個名稱錯誤,我不能確定如何解決。名稱錯誤瓶+ Ngnix + uWSGI

我的代碼:

import logging 
from logging.handlers import RotatingFileHandler 
import json 
from time import gmtime, strftime 
from flask import Flask, request 
from jester import jester 

app = Flask(__name__) 

# initialize the log handler 
logHandler = RotatingFileHandler('/var/log/jester.log', maxBytes=1000, backupCount=1) 
# set the log handler level 
logHandler.setLevel(logging.ERROR) 
# set the app logger level 
app.logger.setLevel(logging.ERROR) 
app.logger.addHandler(logHandler) 

def render_response(response_data, status_code): 
    return app.response_class(
      response=json.dumps(response_data), 
      status=status_code, 
      mimetype='application/json' 
     ) 

# Health check 
@app.route('/health-check') 
def health_check(): 
    return render_response("Healthy", 200) 

# Main ingestion endpoint 
@app.route('/ingest', methods=['POST']) 
def ingest(): 
    # Gather data from the request 
    data = request.get_json() 

    app.logger.info("Running with data: %s" % str(data)) 

    #other code... 

if __name__ == "__main__": 
    app.run(
     host = "0.0.0.0", 
     port = int("9999") 
    ) 

我收到的錯誤是這樣:

[2017-08-30 18:16:26,681] ERROR in app: Exception on /ingest [POST] 
Traceback (most recent call last): 
    File "/var/www/jester/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app 
    response = self.full_dispatch_request() 
    File "/var/www/jester/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "/var/www/jester/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "/var/www/jester/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "/var/www/jester/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "/var/www/jester/server.py", line 28, in ingest 
    return render_response("Healthy", 200) 
NameError: global name 'logger' is not defined 

我uwsgi.ini文件:

[uwsgi] 
#application's base folder 
base = /var/www/jester 

#python module to import 
app = server 
module = %(app) 

home = %(base)/venv 
pythonpath = %(base) 

#socket file's location 
socket = /var/www/jester/%n.sock 

#permissions for the socket file 
chmod-socket = 666 

#the variable that holds a flask application inside the module imported at line #6 
callable = app 

#location of log files 
logto = /var/log/uwsgi/%n.log 

我不明白爲什麼無論我在何處配置記錄器,我都會不斷收到名稱錯誤。 uwsgi是否直接調用路由,以至於它沒有獲取任何日誌配置?我應該如何在這樣一種方式配置日誌記錄,它是可用的應用程序時,以這種方式被服務?

回答

0

您應該初始化logger變量作爲日誌記錄類的對象,然後將handler添加到它。

logger = logging.getLoger() 

在你的logHandler = RotatingFileHandler之上插入這行代碼,你應該沒問題。

您可以對日誌看看this simple tutorial