I find myself only wanting certain fields included in my logging at a few select log levels. Something like:
class ProcessAtLogLevels:
"""Evaluate a chain of processors at a given set log levels.
Args:
levels: Log levels at which to evaluate processor chain.
processors: A chain of *structlog* processors that is used to process entries at the given log levels.
"""
def __init__(self, levels: int | Collection[int], *, processors: Sequence[Processor] = ()):
self._levels = levels if isinstance(levels, Collection) else (levels,)
self._processors = processors
def __call__(self, logger: WrappedLogger, method_name: str, event_dict: EventDict) -> EventDict:
if structlog.processors.NAME_TO_LEVEL[method_name] in self._levels:
for processor in self._processors:
event_dict = processor(logger, method_name, event_dict)
return event_dict
I'll end up with something like this in my processor chain:
I find myself only wanting certain fields included in my logging at a few select log levels. Something like:
I'll end up with something like this in my processor chain:
This will only add line numbers and pathname at the DEBUG and CRITICAL log levels.