structlog icon indicating copy to clipboard operation
structlog copied to clipboard

Ignore frames in `_FixedFindCallerLogger`

Open wochinge opened this issue 1 year ago • 2 comments

Hi y'all,

I'm currently writing a wrapper for the standard logger to make the log calls look a certain way.

The problem is that my wrapper ends up in the stack trace when adding information like line number / calling module to the log.

The standard library allows fixing this by increasing the stack level:

logger = logging.getLogger()
logger.info("Hi, stacklevel=2)

Structlog does something similar when providing the parameter "ignore_frame_names" for the structlog.stdlib.LoggerFactory. Problem is that the LoggerFactory sets a LoggerClass which ignores the stacklevel number and which is not parametrizable.

it would be great if the ignored frames could be changed without overriding stuff or (probably the better solution) the stacklevel param would be passed on and handled correctly.

Happy to contribute once we agree on a solution!

wochinge avatar Feb 28 '24 16:02 wochinge

Hmm, this is once again very awkward because we have to intercept things that stdlib is doing. :/

I would say that CallsiteParameterAdder would be the cleaner choice but I presume you need deeper stdlib support?

If I see it correctly, our loggers call it from https://github.com/hynek/structlog/blob/555c3878a98e5b95986ca5cc36959d4eeaff8397/src/structlog/stdlib.py#L307-L313

I suppose we could add a stacklevel argument – the question is how to signal it from the outside. Starting to interpret stacklevel in all loggers would be a breaking change?

hynek avatar Mar 05 '24 09:03 hynek