Skip to content

Feature Request: Allow deeper customization of locals hiding behavior in ExceptionDictTransformer by supporting callbacks for show_locals #789

@sirosen

Description

@sirosen

I was exploring how the various flags for suppressing / showing / controlling locals behave and had an idea.

Let's suppose I know that there's a certain class of data, trivially identifiable, that my application handles and which I don't want to log. e.g., tokens of some variety.

def is_token(value: str) -> bool:
    return len(value) == 42 and value.startswith(("SPAM:", "EGGS:"))

How can I setup an ExceptionDictTransformer to hide all locals which pass is_token()? I don't think it's easily possible today. But this seems like a valid kind of thing to want to do.

In terms of the current API, I see the options like this:

  • suppress is a module-level filter
  • locals_hide_dunder and locals_hide_sunder are variable-level filters
  • locals_max_string and locals_max_length are variable formatting parameters

Can we phrase these three categories of things as user-controllable hooks or callbacks, where the current behaviors are just "defaults"?

Here's one example of how this might be done, to at least show that it's possible (but results in a bad API):

  1. suppress is the module filter, and could be made into suppress: bool | Callable[[str], bool] -- give it a module filename and give back a bool
  2. locals_hide_dunder and locals_hide_sunder could be augmented with locals_hide_filter: None | Callable[[bool, bool, <...other args...>], bool] -- where those first two bools are the locals_hide_sunder and locals_hide_dunder values
  3. locals_format_repr: None | Callable[int, int, <...other args...>] similarly could take the max string and container lengths

But it shows that it's possible.
Maybe ExceptionDictTransformer could be made to hold composable pieces which would be reusable so that users could build myself such a transformer themselves? Or it could inherit (😬) from a more generic base.

I'm curious if this idea is worth discussing and pursuing. I'd be more than happy to work on it if so.

I didn't see a prior issue for this. Sorry if I missed one. And, of course, thanks so much for structlog. ❤️

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