[英]How to see graphene-django DEBUG logs
我在使用 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
記錄器引起的,但是我在這個方向上的探索沒有任何結果:(
這是我用來捕獲 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
基於@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.