我在使用Graphene和Django查看DEBUG
级别日志时遇到麻烦。我在settings.py
中设置了以下内容:
记录= {'版本':1,'disable_existing_loggers':否,“处理程序”:{'安慰': {'class':'logging.StreamHandler',},},“记录器”:{'django':{'handlers':['console'],'level':'DEBUG'},'django.request':{'handlers':['console'],'level':'DEBUG'},},}
但是,当我尝试查看Django服务器的日志时,看到的只是:
❯❯❯kubectl日志-f服务器-6b65f48895-bmp6w服务器要执行的操作:应用所有迁移:admin,auth,contenttypes,django_celery_beat,django_celery_results,服务器,会话,social_django运行迁移:没有迁移申请。正在执行系统检查...系统检查未发现任何问题(0静音)。十月08,2018-23:59:00Django版本2.0.6,使用设置“ backend.settings”在http://0.0.0.0:8000/上启动开发服务器用CONTROL-C退出服务器。“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113“ POST / graphql HTTP / 1.1” 400113
我如何查看DEBUG
级日志以找出为什么我的服务器持续服务400秒钟?
我未设置Django DEBUG
环境变量。我正在尝试调试生产问题。
不久前,我遇到了同样的问题,并找到了解决方法:
from promise import is_thenable
class DebugMiddleware(object):
def on_error(self, error):
print(error)
def resolve(self, next, root, info, **args):
result = next(root, info, **args)
if is_thenable(result):
result.catch(self.on_error)
return result
并告诉graphene
将其用作中间件:
GRAPHENE = {
...
'MIDDLEWARE': [
'path.to.containing.module.DebugMiddleware',
...
]
}
这里您可以访问因解决而引发的错误。
最初的问题(没有模块记录)可能是由禁用的graphql
记录器引起的,但是我在这个方向上的探索没有任何结果:(
这里是一个快速的中间件,我将其聚集在一起以捕获graphql查询400个错误。
在settings.py
中
MIDDLEWARE = [
"path_to_file_below.GraphqlErrorLogMiddleware",
...
]
# Some basic logging from the Django Documentation
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"root": {"handlers": ["console"], "level": "DEBUG"},
}
class GraphqlErrorLogMiddleware(object):
"""
Logs errors for invalid graphql queries
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
try:
if (
400 >= response.status_code
and response.status_code != 403
and "graphql" in request.path.lower()
):
response_json = json.loads(response.content)
if "errors" in response_json:
log_response(
message=f"Graphql Error: {response_json['errors']}",
response=response,
level="error",
)
except Exception as e:
logging.debug(f"Error logging Graphql Error: {e}")
return response