pythonloguru

Python - loguru disable traceback on exceptions


I'm not able to disable the traceback on exceptions even after setting LOGURU_BACKTRACE environment variable as False. I've also tried logger.configure() method like this.

logger.configure(handlers=[
    {"sink": sys.stderr, "backtrace": False}
])

but loguru is still showing tracebacks on exceptions. Could someone guide me to disable the traceback?


Solution

  • The backtrace attribute controls the length of the traceback (if enabled, Loguru displays the entire traceback instead of stopping at the try/except frame like the standard exception does).

    However, Loguru respects the sys.tracebacklimit value. You can disable traceback by settings it to 0:

    import sys
    
    sys.tracebacklimit = 0
    

    If advanced exception formatting configuration is required, you can define your own way of displaying exceptions by using a custom format function as explained in the documentation: Customizing the formatting of exceptions.