strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Custom logger

Open alexei opened this issue 2 years ago • 2 comments

Strawberry logs all exceptions as errors by default. Unfortunatelly that is both wrong, and generates a lot of noise. I would hereby like to propose to update the schema class to allow one to specify a custom logger.

Current implementation:

class StrawberryLogger:
    logger

    @classmethod
    def error(cls, ...):
        cls.logger.error(...)
        ...


class Schema:
    ...

    def process_errors(self, errors, execution_context):
        StrawberryLogger.error(error, execution_context)
        ...

Proposed change:

class StrawberryLogger:
    logger

    # note this is now an instance method
    def error(self, ...):
        self.logger.error(...)
        ...


class Schema:
    def __init__(self, ..., logger):
        self.logger = logger or StrawberryLogger()

    def process_errors(self, errors, execution_context):
        self.logger.error(error, execution_context)
        ...

This would allow one to extend the built-in StrawberryLogger as they please/need. FWIW in my case, I'd override the error method to make it more discriminate.

alexei avatar Feb 06 '23 19:02 alexei

This is desperately needed and a good idea. I take a b64encoded image as a parameter in one of my requests and the logging when there is an error is pretty annoying.

colonelpanic8 avatar May 13 '25 23:05 colonelpanic8

Hrm I think this makes a lot of sense, and personally I like the suggested API. Wdyt @patrick91 ?


In the meantime, anyone in need to customize their logging can do that by overriding the schema class, like this:

class MySchema(strawberry.Schema):
    def process_errors(
        self,
        errors: list,
        execution_context: Optional[ExecutionContext] = None,
    ) -> None:
        ...  # custom code to handle/log the errors


schema = MySchema(...)

I have done this in the past to exempt some errors from going to sentry for example.

bellini666 avatar May 24 '25 09:05 bellini666