gpt4 book ai didi

django - 如何发出 Django LiveServerTestCase 日志请求

转载 作者:行者123 更新时间:2023-12-04 23:43:04 26 4
gpt4 key购买 nike

Django 有一个特性到 log each http request .例如。:
INFO 2017-08-31 11:52:40,725 arnaud basehttp "GET /design/projects/ HTTP/1.1" 200 3852
不幸的是,它似乎在运行 LiveServerTestCase 时被禁用了。 .为了简化端到端测试的调试,我希望此功能保持激活状态。

最佳答案

事实证明,这种行为似乎不可配置。我找不到有关该主题的任何文档,于是求助于 Django 内部。
解决方案
( 免责声明 :这是非官方的,因此很脆弱)
覆盖 server_thread_class您的 LiveServerTestCase 的属性

from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
from django.test.testcases import LiveServerTestCase, LiveServerThread

class End2End(LiveServerTestCase):

class VersboseLiveServerThread(LiveServerThread):
def _create_server(self):
return ThreadedWSGIServer((self.host, self.port), WSGIRequestHandler, allow_reuse_address=False)

server_thread_class = VersboseLiveServerThread
更多细节
此解决方案以及无法正确配置该行为的感觉源于观察 LiveServerTestCase构建(Django 1.11):
class LiveServerTestCase(TransactionTestCase):
# [...]
server_thread_class = LiveServerThread

class LiveServerThread(threading.Thread):
# [...]
def _create_server(self):
# an extension point returning the WSGIRequestHandler would have been nice :)
return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)

class QuietWSGIRequestHandler(WSGIRequestHandler):
def log_message(*args):
pass

关于django - 如何发出 Django LiveServerTestCase 日志请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45981634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com