Delgan / loguru

Python logging made (stupidly) simple
MIT License
20.04k stars 702 forks source link

`diagnose` flag doesn't work. #1235

Closed lifr0m closed 4 days ago

lifr0m commented 5 days ago
Python 3.13.0
loguru 0.7.2

This code

from loguru import logger

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception('')

func(1234, 0)

Prints

2024-11-20 18:13:47.612 | ERROR    | __main__:func:7 - 
Traceback (most recent call last):

  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code

  File "/Users/lifr0m/code/test/src/first.py", line 9, in <module>
    func(1234, 0)

> File "/Users/lifr0m/code/test/src/first.py", line 5, in func
    a / b

ZeroDivisionError: division by zero

I found that in file /_better_exceptions.py on line 500 traceback.format_list(frames) removes variables' state.

This code

import traceback

part = '\x1b[1ma\x1b[0m \x1b[35m\x1b[1m/\x1b[0m \x1b[1mb\x1b[0m\n    \x1b[36m│   └ \x1b[0m\x1b[36m\x1b[1m0\x1b[0m\n    \x1b[36m└ \x1b[0m\x1b[36m\x1b[1m1234\x1b[0m'

print(part)

print('--------------------------------------------------------')

print(traceback.format_list([
    ('somefile.py', 42, 'somefunc', part)
])[0])

Prints

a / b
    │   └ 0
    └ 1234
--------------------------------------------------------
  File "somefile.py", line 42, in somefunc
    a / b
Delgan commented 5 days ago

The shared snippet works as expected when executed using the python command:

programming/loguru $ python a.py 
2024-11-20 16:25:33.520 | ERROR    | __main__:func:8 - 
Traceback (most recent call last):

  File "/home/delgan/programming/loguru/a.py", line 11, in <module>
    func(1234, 0)
    └ <function func at 0x7d44879ea340>

> File "/home/delgan/programming/loguru/a.py", line 6, in func
    a / b
    │   └ 0
    └ 1234

ZeroDivisionError: division by zero
programming/loguru $ 

I see references to <frozen runpy> in the traceback you shared. How is your script executed?

lifr0m commented 5 days ago

The shared snippet works as expected when executed using the python command:

programming/loguru $ python a.py 
2024-11-20 16:25:33.520 | ERROR    | __main__:func:8 - 
Traceback (most recent call last):

  File "/home/delgan/programming/loguru/a.py", line 11, in <module>
    func(1234, 0)
    └ <function func at 0x7d44879ea340>

> File "/home/delgan/programming/loguru/a.py", line 6, in func
    a / b
    │   └ 0
    └ 1234

ZeroDivisionError: division by zero
programming/loguru $ 

I see references to <frozen runpy> in the traceback you shared. How is your script executed?

In PyCharm. Here zsh terminal execution:

➜  Desktop cat a.py 
from loguru import logger

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception('')

func(1234, 0)
➜  Desktop python3 --version
Python 3.13.0
➜  Desktop python3 a.py 
2024-11-20 20:46:33.073 | ERROR    | __main__:func:7 - 
Traceback (most recent call last):

  File "/Users/lifr0m/Desktop/a.py", line 9, in <module>
    func(1234, 0)

> File "/Users/lifr0m/Desktop/a.py", line 5, in func
    a / b

ZeroDivisionError: division by zero
➜  Desktop 
Delgan commented 5 days ago

Thanks for the additional details.

Can you please share the output of the following script using the python3 command again:

from loguru import logger
import os
import sys

print("Diagnose: ", os.environ.get("LOGURU_DIAGNOSE", None))

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception("")

logger.remove()
logger.add(sys.stderr, diagnose=True)
func(1234, 0)
lifr0m commented 4 days ago

Thanks for the additional details.

Can you please share the output of the following script using the python3 command again:

from loguru import logger
import os
import sys

print("Diagnose: ", os.environ.get("LOGURU_DIAGNOSE", None))

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception("")

logger.remove()
logger.add(sys.stderr, diagnose=True)
func(1234, 0)
➜  Desktop cat a.py 
from loguru import logger
import os
import sys

print("Diagnose: ", os.environ.get("LOGURU_DIAGNOSE", None))

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception("")

logger.remove()
logger.add(sys.stderr, diagnose=True)
func(1234, 0)
➜  Desktop python3 a.py 
Diagnose:  None
2024-11-21 17:22:14.186 | ERROR    | __main__:func:12 - 
Traceback (most recent call last):

  File "/Users/lifr0m/Desktop/a.py", line 17, in <module>
    func(1234, 0)

> File "/Users/lifr0m/Desktop/a.py", line 10, in func
    a / b

ZeroDivisionError: division by zero
➜  Desktop 
lifr0m commented 4 days ago

Thanks for the additional details.

Can you please share the output of the following script using the python3 command again:

from loguru import logger
import os
import sys

print("Diagnose: ", os.environ.get("LOGURU_DIAGNOSE", None))

def func(a: int, b: int) -> None:
    try:
        a / b
    except Exception:
        logger.exception("")

logger.remove()
logger.add(sys.stderr, diagnose=True)
func(1234, 0)

As I thought, it's Python 3.13 problem.

➜  Desktop python3.13 --version
Python 3.13.0
➜  Desktop python3.12 --version
Python 3.12.7
➜  Desktop python3.13 a.py     
Diagnose:  None
2024-11-21 17:24:03.132 | ERROR    | __main__:func:12 - 
Traceback (most recent call last):

  File "/Users/lifr0m/Desktop/a.py", line 17, in <module>
    func(1234, 0)

> File "/Users/lifr0m/Desktop/a.py", line 10, in func
    a / b

ZeroDivisionError: division by zero
➜  Desktop python3.12 a.py     
Diagnose:  None
2024-11-21 17:24:05.285 | ERROR    | __main__:func:12 - 
Traceback (most recent call last):

  File "/Users/lifr0m/Desktop/a.py", line 17, in <module>
    func(1234, 0)
    └ <function func at 0x102f32160>

> File "/Users/lifr0m/Desktop/a.py", line 10, in func
    a / b
    │   └ 0
    └ 1234

ZeroDivisionError: division by zero
➜  Desktop 
Delgan commented 4 days ago

Thanks for the investigations on your side, @lifr0m. That's very helpful since I can't reproduce it and have no clue for now regarding the cause of such issue.

Loguru is tested against Python 3.13 and it is supposed to work fine (including exception formatting tests): https://github.com/Delgan/loguru/actions/runs/11874268181/job/33090019768

Same on my Linux computer:

programming/loguru $ uv run -p python313 a.py
warning: `VIRTUAL_ENV=env` does not match the project environment path `.venv` and will be ignored
Python version: 3.13.0 (main, Oct 16 2024, 03:23:02) [Clang 18.1.8 ]
Diagnose:  None
2024-11-21 15:28:15.393 | ERROR    | __main__:func:13 - 
Traceback (most recent call last):

  File "/home/delgan/programming/loguru/a.py", line 18, in <module>
    func(1234, 0)
    └ <function func at 0x7121083272e0>

> File "/home/delgan/programming/loguru/a.py", line 11, in func
    a / b
    │   └ 0
    └ 1234

ZeroDivisionError: division by zero
programming/loguru $

May I ask more information about your OS and how you installed Python 3.13 please? That might be related to some build options.

lifr0m commented 4 days ago

@Delgan Device: MacBook Air M1 OS: MacOS Sequoia 15.1 Installed through: Brew (brew install python)

lifr0m commented 4 days ago

@Delgan here is formula: https://formulae.brew.sh/formula/python@3.13. Formula JSON API and Formula code there might be helpful.

Delgan commented 4 days ago

Thanks for the additional details.

Now, I remember this is a bug with 0.7.2 version of Loguru and I was able to reproduce it locally on Linux. Fortunately, this has been fixed thanks to @etianen's excellent work in #1079.

However, since no release were published since then, the fix is only available on the master branch of Loguru. :grimacing:

If you have the possibility, I recommend installing Loguru from the latest repo commit, for example:

pip install git+https://github.com/delgan/loguru@3ee4ef3dfd95f0bebdf87530963e3253d5410e5d

For my part, I'm going to try to publish a new version soon.