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):
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
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
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. ❤️
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.
How can I setup an
ExceptionDictTransformerto hide all locals which passis_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:
suppressis a module-level filterlocals_hide_dunderandlocals_hide_sunderare variable-level filterslocals_max_stringandlocals_max_lengthare variable formatting parametersCan 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):
suppressis the module filter, and could be made intosuppress: bool | Callable[[str], bool]-- give it a module filename and give back a boollocals_hide_dunderandlocals_hide_sundercould be augmented withlocals_hide_filter: None | Callable[[bool, bool, <...other args...>], bool]-- where those first two bools are thelocals_hide_sunderandlocals_hide_dundervalueslocals_format_repr: None | Callable[int, int, <...other args...>]similarly could take the max string and container lengthsBut it shows that it's possible.
Maybe
ExceptionDictTransformercould 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. ❤️