烧瓶应用的印刷顺序不一致

时间:2018-05-12 13:09:58

标签: python

每次向此网址发出请求时,程序都会不一致地打印错误。此错误跟踪的最后两行打印一些调试信息。打印行包括文本SCHEMA和JSON。它到处都不适合我的调试方式,如何确保一致的打印顺序?

[2018-05-12 22:59:09,766] ERROR in app: Exception on /test [GET]
Traceback (most recent call last):
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/matt/PycharmProjects/WebService/app.py", line 13, in test
    validation.validate("{'asdf':'testing title'}")
  File "/Users/matt/PycharmProjects/WebService/model/series/seriesValidator.py", line 21, in validate
    ModelValidator.validate(self, json)
  File "/Users/matt/PycharmProjects/WebService/validate.py", line 16, in validate
    errors = [e for e in validator.iter_errors(json)]
AttributeError: 'NoneType' object has no attribute 'iter_errors'
JSON:  {'asdf':'testing title'}
SCHEMA:  {'$schema': 'http://json-schema.org/schema#', 'required': ['title'], 'type': 'object', 'properties': {'description': {'type': 'string'}, 'title': {'type': 'string'}}}
127.0.0.1 - - [12/May/2018 22:59:09] "GET /test HTTP/1.1" 500 -
J

然后我又提出了2个请求而且不一致:

JSON:  {'asdf':'testing title'}
SCHEMA:  {'$schema': 'http://json-schema.org/schema#', 'required': ['title'], 'type': 'object', 'properties': {'description': {'type': 'string'}, 'title': {'type': 'string'}}}
[2018-05-12 22:59:18,102] ERROR in app: Exception on /test [GET]
Traceback (most recent call last):
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/matt/PycharmProjects/WebService/app.py", line 13, in test
    validation.validate("{'asdf':'testing title'}")
  File "/Users/matt/PycharmProjects/WebService/model/series/seriesValidator.py", line 21, in validate
    ModelValidator.validate(self, json)
  File "/Users/matt/PycharmProjects/WebService/validate.py", line 16, in validate
    errors = [e for e in validator.iter_errors(json)]
AttributeError: 'NoneType' object has no attribute 'iter_errors'
127.0.0.1 - - [12/May/2018 22:59:18] "GET /test HTTP/1.1" 500 -
JSON:  {'asdf':'testing title'}
[2018-05-12 22:59:42,329] ERROR in app: Exception on /test [GET]
Traceback (most recent call last):
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
SCHEMA:  {'$schema': 'http://json-schema.org/schema#', 'required': ['title'], 'type': 'object', 'properties': {'description': {'type': 'string'}, 'title': {'type': 'string'}}}
    reraise(exc_type, exc_value, tb)
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/matt/PycharmProjects/WebService/venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/matt/PycharmProjects/WebService/app.py", line 13, in test
    validation.validate("{'asdf':'testing title'}")
  File "/Users/matt/PycharmProjects/WebService/model/series/seriesValidator.py", line 21, in validate
    ModelValidator.validate(self, json)
  File "/Users/matt/PycharmProjects/WebService/validate.py", line 16, in validate
    errors = [e for e in validator.iter_errors(json)]
AttributeError: 'NoneType' object has no attribute 'iter_errors'
127.0.0.1 - - [12/May/2018 22:59:42] "GET /test HTTP/1.1" 500 -

1 个答案:

答案 0 :(得分:2)

do转到print,追溯并记录转到stdout。这两个流经常混在一起。如果您使用正确的日志记录,那么它将全部在stderr上,一切都会很好。

在文件顶部,执行:

stderr

然后执行:

而不是打印
import logging

log = logging.getLogger(__name__)

您可能还必须配置日志记录以在控制台中查看输出。