Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.82k stars 697 forks source link

Loguru infinite recursion traceback loop #1044

Open mpenning opened 10 months ago

mpenning commented 10 months ago

My problem is if I hit a bug in certain methods, I get an infinite traceback from loguru. I love loguru when it works, but digging into the infinite recursion is a serious drag on my development because even with a multi-thousand-line scrollback buffer, I still can't hit control-c fast enough to interrupt the infinite loop of loguru messages and find the real cause of the problem (i.e. I have two problems, loguru inifinte recursion and the ciscoconfparse2 bug that caused it).

Is there a way to get loguru to stop printing tracebacks in an infinite loop?

You can reproduce the infinite recursion loop as follows:

from loguru import logger

from ciscoconfparse2 import CiscoConParse

parse = CiscoConfParse("tests/fixtures/configs/sample_01.junos", syntax="junos", factory=False)
print(parse)
for obj in parse.objs:
    print(obj)
Delgan commented 10 months ago

Hi @mpenning.

Sorry you're experiencing difficulties with Loguru, and thanks for the reproducible example. It boils down to the following:

from loguru import logger

class Foo:
    @logger.catch(reraise=True)
    def __repr__(self):
        raise ValueError("Something went wrong")

if __name__ == "__main__":
    foo = Foo()
    print(foo)

It's definitely a problem with diagnose=True which causes infinite recursion if the exception is raised in __repr__, since v can't be printed: https://github.com/Delgan/loguru/blob/dcf42d962567723c0195bb8b2bb6fd764ecf41ae/loguru/_better_exceptions.py#L331-L335

I'll try to implement a fix. In the meantime, I advise to use diagnose=False or remove @logger.catch() decorator from __repr__() methods.