简体   繁体   English

如何查看graphene-django DEBUG日志

[英]How to see graphene-django DEBUG logs

I'm having trouble viewing DEBUG level logs with Graphene and Django.我在使用 Graphene 和 Django 查看DEBUG级别日志时遇到问题。 I've set the following in settings.py :我在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'
        },
    },
}

However, when I try to look at the logs of my Django server all I see is:但是,当我尝试查看 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

How can I view DEBUG level logs to figure out why my server is constantly serving 400s?如何查看DEBUG级别的日志以找出为什么我的服务器持续服务 400 秒?

I have the Django DEBUG environment variable unset.我没有设置 Django DEBUG环境变量。 I'm trying to debug a production issue.我正在尝试调试生产问题。

I was messed with the same question a while ago and came to the workaround solution:前一阵子我被同样的问题搞砸了,来到了解决方法:

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

And tell graphene to use it as middleware:并告诉graphene将其用作中间件:

GRAPHENE = {
    ...
    'MIDDLEWARE': [
        'path.to.containing.module.DebugMiddleware',
        ...
    ]
}

Here you get access to an error trowed on resolve.在这里,您可以访问解决时出现的错误。

Initial problem (no module logging) may be caused by disabled graphql logger, but my explorations in this direction didn't have any results :(最初的问题(没有模块日志记录)可能是由于禁用了graphql记录器引起的,但是我在这个方向上的探索没有任何结果:(

Here's a quick middleware I threw together to capture graphql query 400 errors.这是我用来捕获 graphql 查询 400 错误的快速中间件。

In settings.pysettings.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

Based on @donnyy answer I came up with the following implementation基于@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM