Delgan / loguru

Python logging made (stupidly) simple
MIT License
18.69k stars 682 forks source link

Can't diagnose=False used in sys.stdout? #1152

Closed qq745639151 closed 2 weeks ago

qq745639151 commented 2 weeks ago

I tried the example, but I set the diagnose=False

logger.add("out.log", backtrace=True, diagnose=False)
def func(a, b):
    return a / b

def nested(c):
    try:
        func(5, c)
    except ZeroDivisionError:
        logger.exception("What?!")

nested(0)

but the output is the same as in the case of diagnose=True.

qq745639151 commented 2 weeks ago

This is the output when diagnose=True

2024-06-15 19:12:27.677 | ERROR    | __main__:nested:15 - What?!
Traceback (most recent call last):

  File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 17, in <module>
    nested(0)
    └ <function nested at 0x000001D782E3D090>

> File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 13, in nested
    func(5, c)
    │       └ 0
    └ <function func at 0x000001D780773E20>

  File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 9, in func
    return a / b
           │   └ 0
           └ 5

ZeroDivisionError: division by zero

and This is the output when diagnose=False

2024-06-15 19:15:01.122 | ERROR    | __main__:nested:15 - What?!
Traceback (most recent call last):

  File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 17, in <module>
    nested(0)
    └ <function nested at 0x000002B83785D090>

> File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 13, in nested
    func(5, c)
    │       └ 0
    └ <function func at 0x000002B834E03E20>

  File "F:\WorkSpace\PycharmProjects\fastapi_learning\async_log.py", line 9, in func
    return a / b
           │   └ 0
           └ 5

ZeroDivisionError: division by zero
Delgan commented 2 weeks ago

The diagnose formatting should work regardless of the logging output.

Are you sure you did remove the default handler? Maybe call logger.remove() first. Also, be aware that if you run multiple FastAPI workers, your main file will be executed multiple times, which can disrupt the configuration of your logger.

qq745639151 commented 2 weeks ago

Thanks for your answer. I have tried and solved the question. Thanks again