Twilio之后激活了Python Logger?

时间:2013-09-07 11:31:04

标签: python logging flask twilio

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

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 -

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

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 -

INFO:werkzeug:前缀让我相信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 -

1 个答案:

答案 0 :(得分: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)