1

I use graphene application in starlette on uvicorn.

I would like to see exceptions logs with traceroute.

But at the moment it hides exceptions and I see only access log with "Bad request" message.

I've redirected all loggers I know to the root logger, except access log.

The root logger is turned to log messages properly, as I want, but it seems exceptions in app handlers are coaught and not propagated to the root logger.

I also have set up sys.excepthook like:

def _exception_logging(exctype, value, tb):
    traceback_string = ''.join(traceback.format_tb(tb, 10))
    write_val = \
        f'Traceback (most recent call last):\n{traceback_string}{exctype.__name__}: {value}'
    logging.error(write_val)

And I set up exception hooks for threads. Usually it's enough to see all log messages.

I use the next json config file:

{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "default": {
      "()": "uvicorn.logging.DefaultFormatter",
      "fmt": "%(levelprefix)s %(message)s",
      "use_colors": null
    },
    "access": {
      "()": "uvicorn.logging.AccessFormatter",
      "fmt": "[%(asctime)s] [%(process)s] [%(levelname)s]: %(client_addr)s - \"%(request_line)s\" %(status_code)s"
    }
  },
  "handlers": {
    "default": {
      "formatter": "default",
      "class": "logging.StreamHandler",
      "stream": "ext://sys.stderr"
    },
    "access": {
      "formatter": "access",
      "class": "logging.StreamHandler",
      "stream": "ext://sys.stdout"
    }
  },
  "loggers": {
    "uvicorn": {
      "level": "INFO"
    },
    "uvicorn.exception": {
      "level": "INFO"
    },
    "uvicorn.error": {
      "level": "INFO"
    },
    "uvicorn.access": {
      "handlers": [
        "access"
      ],
      "level": "INFO",
      "propagate": false
    }
  }
}

I suspect something is wrong in the log config file.

Роман Коптев
  • 1,555
  • 1
  • 13
  • 35

1 Answers1

0

It was graphql-python library specific.

The graphql.format_error function should be overwritten for example like this:

import logging
from graphql import format_error as default_format_error

def format_error(error):
    try:
        original_error = error.original_error
        if isinstance(original_error, Exception):
            logging.error('Error in GraphQL view', exc_info=original_error)
    finally:
        return default_format_error(error)
Роман Коптев
  • 1,555
  • 1
  • 13
  • 35