Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exc_info keyword when logging an exception. #165

Closed
wants to merge 1 commit into from

Conversation

CaselIT
Copy link
Contributor

@CaselIT CaselIT commented Mar 17, 2018

An improvement of my older PR graphql-python/graphql-core#154 which replaced sys.excepthook with the logging module.
Instead of manually formating the exception use the exc_info keyword to let the logging library handle the formatting, and properly set the exc_info property of the log record.

As @tlinhart pointed out in graphql-python/graphql-core#142 with the current implementation is difficult to filter which exceptions are logged.

This allows easier filtering of exceptions. Example:

class Filter(logging.Filter):
    def filter(self, record):
        if record.exc_info:
            exception = record.exc_info[1]
            # exception is an instance of `GraphQLLocatedError`, the original error is in the property `original_error`
            return not isinstance(exception.original_error, IgnoredException)
        return True

logging.getLogger('graphql.execution.base').addFilter(Filter())

@tlinhart
Copy link

Thanks a lot @CaselIT!

@syrusakbary syrusakbary force-pushed the master branch 3 times, most recently from b1f26c1 to a7ce75e Compare July 19, 2018 18:24
@mathiasose
Copy link

Any update on this?

I was just digging through the source code looking for a way to solve this exact problem, then I found this PR. It looks like just the thing I need, but sadly it has been open since March 17th and not merged and released.
Is there anything blocking it?

@CaselIT There's a merge conflict now, maybe you can solve it on your branch to make the job even easier for the maintainers?

@ArnaudPel
Copy link

ArnaudPel commented Sep 26, 2018

I've also been losing too much precious time today trying to figure out how to make graphql stop swallowing exception tracebacks. This PR does look like a nice fix indeed, but I'm afraid the rabbit hole might go deeper if you use middlewares: #208

The consequence (at least for the case I've dug) is that report_error() is left without any traceback at all. getattr(error, "stack", None) or traceback is None.

Anyway, at the moment, disabling all middlewares fixed it for me. This may not be the right place to put this last workaround, but what the hell. Here's what I did in my django settings module:

GRAPHENE = {
    'SCHEMA': 'awsome.schema',
    'MIDDLEWARE': []  # <= added this line
}

@CaselIT
Copy link
Contributor Author

CaselIT commented Sep 26, 2018

@mathiasose I've updated the pull request to remove the conflicts.
It has been so long that I forgot this was still open.

I'm not sure why it has not been reviewed or merged

This allows easier filtering of exception with logging filters
@CaselIT
Copy link
Contributor Author

CaselIT commented Sep 26, 2018

The tests seem to randomly fail. I've replaced the old commits with a single one, so the history is clearer

@CaselIT
Copy link
Contributor Author

CaselIT commented Dec 10, 2019

closing this, since there seems to not be any interest in merging it

@CaselIT CaselIT closed this Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants