django - 如何查看 graphene-django 调试日志

标签 django graphql graphene-python

我在使用 Graphene 和 Django 查看 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'
        },
    },
}

但是,当我尝试查看 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

如何查看 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 记录器引起的,但我在这个方向上的探索没有任何结果:(

关于django - 如何查看 graphene-django 调试日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52711580/

相关文章:

python - Django 。覆盖模型的保存

python - Windows 上的 mod_wsgi WSGIPythonPath 多路径

python - django-graphene 更改列名称弃用

django - timezone.now().date 是函数还是可调用的?

python - 在 Django 中显示用户的所有帖子

github - 从 GitHub 存储库获取文件信息的 GraphQL 查询

python - 在突变中传递链接数组 [graphene/python/graphql]

java - 拦截器在 Spring Boot GraphQL 中不起作用

python - Django- Graphite 烯 : how to filter with an OR operator

django - 针对 django graphene 上的 graphql 查询返回带注释的查询集