Pythonrequests
ライブラリには、ログの動作に関して、かなり奇妙な癖があるようです。最新の Python 2.7.8 を使用して、次のコードを作成しました。
import requests
import logging
logging.basicConfig(
filename='mylog.txt',
format='%(asctime)-19.19s|%(task)-36s|%(levelname)s:%(name)s: %(message)s',
level=eval('logging.%s' % 'DEBUG'))
logger = logging.getLogger(__name__)
logger.info('myprogram starting up...', extra={'task': ''}) # so far so good
...
(ommited code)
...
payload = {'id': 'abc', 'status': 'ok'}
# At this point the program continues but throws an exception.
requests.get('http://localhost:9100/notify', params=payload)
print 'Task is complete! NotifyURL was hit! - Exiting'
私のプログラムは正常に終了したように見えますが、作成されるログ ファイル (mylog.txt) 内で常に次の例外を見つけます。
KeyError: 'task'
Logged from file connectionpool.py, line 362
これを削除すると
requests.get('http://localhost:9100/notify', params=payload)
、例外はなくなります。
ここで正確に何が間違っているのですか?どうすれば修正できますか? リクエスト v2.4.3 を使用しています。