Skip to content

Suppressing the KeyError for an unbind() call #171

@anirvan-majumdar

Description

@anirvan-majumdar

Hi,

This is a suggestion for possible improvement in the logic of invoking the unbind() method on the BoundLogger. We recently deployed structlog in our Django project. Everything seemed to work fine until we started getting a bunch of KeyErrors while unbinding parameters.

We realised that we had a recursive logic where on a child stack the context variable would get unbound and when the control came back to the parent stack, another call to unbind crashed the flow.

For now, we added a new wrapper class over the BoundLogger which simply suppresses the exception:

from structlog.stdlib import BoundLogger


class BoundLoggerWrapper(BoundLogger):
    def unbind(self, *keys):
        try:
            return super(BoundLoggerWrapper, self).unbind(*keys)
        except KeyError:
            return self.bind()

Just wanted to check whether doing this might have any unwanted consequences.

Thanks for this wonderful library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions