0

我创建了一个标准的准系统烧瓶应用程序,它输出标准请求行,如下所示:

127.0.0.1 - - [07/Sep/2013 13:23:34] "GET /static/lib/jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:34] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:34] "GET /static/lib/md5.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:34] "GET /static/lib/gmaps.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:34] "GET /static/lib/ckeditor/ckeditor.js HTTP/1.1" 304 -

但是,一旦我TwilioRestClientpython Twilio驱动程序初始化一个实例,日志记录就开始看起来像这样

127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/jquery.js HTTP/1.1" 304 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 304 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/bootstrap/js/bootstrap.min.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/md5.js HTTP/1.1" 304 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/md5.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/gmaps.js HTTP/1.1" 304 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/gmaps.js HTTP/1.1" 304 -
127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/ckeditor/ckeditor.js HTTP/1.1" 304 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 13:23:55] "GET /static/lib/ckeditor/ckeditor.js HTTP/1.1" 304 -

前缀让我相信 werkzeug 使用默认的INFO:werkzeug:python 记录器,但没有“启用”它,但 Twilio 进来并打开它。我敢肯定这不是它的工作原理,但你明白了。

初始化 Twilio 后是否必须手动禁用 python 记录器?怎么会这样?我可以只为 Twilio 启用它吗?

谢谢!

更新:重现问题

twilio_test.py

from flask import Flask
from twilio.rest import TwilioRestClient

app = Flask(__name__)

twilio_sid = 'TWILIO_SID'
twilio_auth_token = 'TWILIO_TOKEN'
origin_number = 'TWILIO_PHONE_NUMBER'
recipient_number = 'YOUR_CELL_NUMBER'

@app.route('/')
def hello():
    return 'Hello!'

@app.route('/twilio/')
def hello_twilio():

    twilio_client = TwilioRestClient(twilio_sid, twilio_auth_token)

    twilio_msg = twilio_client.sms.messages.create(to=recipient_number, from_=origin_number, body='Twilio test script')

    return 'Hello Twilio!'

if __name__ == '__main__':
    app.run(debug=True)

输出(注意对 /twilio/ 的唯一请求在第 4 行)

127.0.0.1 - - [07/Sep/2013 17:07:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:18] "GET /twilio/ HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 17:07:18] "GET /twilio/ HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:20] "GET / HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 17:07:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:21] "GET / HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 17:07:21] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2013 17:07:21] "GET / HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [07/Sep/2013 17:07:21] "GET / HTTP/1.1" 200 -
4

1 回答 1

0

看起来问题是logging.<x>导致重复错误的额外调用。我能够用这个要点重现这个问题(没有 Twilio):

https://gist.github.com/kevinburke/6477126

如果您logging在应用程序顶部导入,Werkzeug 将不会有两条日志消息。

Werkzeug 中的相关代码部分在这里:

https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/_internal.py#L77

你想在你的应用程序顶部使用这样的东西来获得一个 Werkzeug/Flask 调试记录器:

import logging
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
于 2013-09-07T16:47:48.277 回答