如何查看graphene django调试日志
我无法使用Graphene和Django查看如何查看graphene django调试日志,django,graphql,graphene-python,Django,Graphql,Graphene Python,我无法使用Graphene和Django查看DEBUG级别日志。我在settings.py中设置了以下内容: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler', }, }, 'loggers': {
DEBUG
级别日志。我在settings.py中设置了以下内容:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'DEBUG'
},
'django.request': {
'handlers': ['console'],
'level': 'DEBUG'
},
},
}
日志记录={
“版本”:1,
“禁用现有日志记录器”:False,
“处理程序”:{
“控制台”:{
'class':'logging.StreamHandler',
},
},
“伐木工人”:{
“django”:{
“处理程序”:[“控制台”],
“级别”:“调试”
},
“django.request”:{
“处理程序”:[“控制台”],
“级别”:“调试”
},
},
}
但是,当我尝试查看Django服务器的日志时,我看到的是:
❯❯❯ kubectl logs -f server-6b65f48895-bmp6w server
Operations to perform:
Apply all migrations: admin, auth, contenttypes, django_celery_beat, django_celery_results, server, sessions, social_django
Running migrations:
No migrations to apply.
Performing system checks...
System check identified no issues (0 silenced).
October 08, 2018 - 23:59:00
Django version 2.0.6, using settings 'backend.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
"POST /graphql HTTP/1.1" 400 113
❯❯❯ kubectl日志-f服务器-6b65f48895-bmp6w服务器
要执行的操作:
应用所有迁移:admin、auth、contenttypes、django_Cellery_beat、django_Cellery_结果、服务器、会话、社交_django
运行迁移:
没有要应用的迁移。
正在执行系统检查。。。
系统检查未发现任何问题(0静音)。
2018年10月8日-23:59:00
Django版本2.0.6,使用“backend.settings”设置
正在启动开发服务器http://0.0.0.0:8000/
使用CONTROL-C退出服务器。
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
“POST/graphql HTTP/1.1”400 113
我如何查看DEBUG
级别的日志,以了解我的服务器为什么持续提供400秒的服务
我没有设置DjangoDEBUG
环境变量。我正在尝试调试一个生产问题。不久前,我被同一个问题弄得一团糟,于是找到了解决方案:
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',
...
]
}
在这里,您可以访问resolve上出现的错误
最初的问题(没有模块日志记录)可能是由于禁用了graphql
logger造成的,但我在这个方向上的探索没有任何结果:(这里是一个快速中间件,我将其组装起来以捕获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"},
}
基于@donnyy的回答,我提出了以下实现
from promise import is_thenable
from functools import partial
import logging
import sys
import json
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
class DebugMiddleware(object):
def on_error(self, error ,info):
log_request_body(info)
def resolve(self, next, root, info, **args):
result = next(root, info, **args)
if is_thenable(result):
result.catch(partial(self.on_error, info=info))
return result
def log_request_body(info):
body = info.context._body.decode('utf-8')
try:
json_body = json.loads(body)
logging.error(' User: %s \n Action: %s \n Variables: %s \n Body: %s',
info.context.user,
json_body['operationName'],
json_body['variables'],
json_body['query'])
except:
logging.error(body)
谢谢你的帮助!你介意分享如何正确地连接吗?什么是“is_thenable”?一个实用函数,用于确定指定的对象是否是承诺。添加了缺少的导入
from promise import is_thenable
from functools import partial
import logging
import sys
import json
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
class DebugMiddleware(object):
def on_error(self, error ,info):
log_request_body(info)
def resolve(self, next, root, info, **args):
result = next(root, info, **args)
if is_thenable(result):
result.catch(partial(self.on_error, info=info))
return result
def log_request_body(info):
body = info.context._body.decode('utf-8')
try:
json_body = json.loads(body)
logging.error(' User: %s \n Action: %s \n Variables: %s \n Body: %s',
info.context.user,
json_body['operationName'],
json_body['variables'],
json_body['query'])
except:
logging.error(body)