Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

No fully descriptive stack trace when using decorator (hashtag659) #64

Closed vikramsubramanian closed 2 weeks ago

vikramsubramanian commented 2 weeks ago

hashtaghashtaghashtaghashtag Environment I use Anaconda and python inside a conda environment. Python version: Python 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 OS: Windows 11 Loguru version: 0.6.0

hashtaghashtaghashtaghashtag Issue description When using the ` decorator on a function I do not get the expected output with the entire stack trace and the values of variables.

hashtaghashtaghashtaghashtag Steps to reproduce the issue

  1. Create a conda env called loguru with
conda create --name loguru python=3.8.12
  1. Activate the new env using
conda activate loguru
  1. Install loguru using pip
pip install loguru
  1. Start a python interpreter right from the console using
python
  1. Run this code (which I took from the [README](
from loguru import logger
logger.add("out.log", backtrace=True, diagnose=True)

def my_function(x, y, z):
    hashtag An error? It's caught anyway!
    return 1 / (x + y + z)
my_function(0, 0, 0)
  1. Observe the console output check if it is as expected.

hashtaghashtaghashtaghashtag What's the expected result?

2018-07-17 01:38:43.975 | ERROR    | __main__:nested:10 - What?!
Traceback (most recent call last):

  File "test.py", line 12, in <module>
    nested(0)
    └ <function nested at 0x7f5c755322f0>

> File "test.py", line 8, in nested
    func(5, c)
    │       └ 0
    └ <function func at 0x7f5c79fc2e18>

  File "test.py", line 4, in func
    return a / b
           │   └ 0
           └ 5

ZeroDivisionError: division by zero

hashtaghashtaghashtaghashtag What's the actual result?

2022-05-23 00:11:02.830 | ERROR    | __main__:<module>:1 - An error has been caught in function '<module>', process 'MainProcess' (12040), thread 'MainThread' (2052):
Traceback (most recent call last):

> File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in my_function

ZeroDivisionError: division by zero

hashtaghashtaghashtaghashtag Additional details / screenshot

![image](

vikramsubramanian commented 2 weeks ago

hashtaghashtaghashtaghashtag Additional Info

Also the log file out.log created by above code has:

2022-05-23 00:11:02.830 | ERROR    | __main__:<module>:1 - An error has been caught in function '<module>', process 'MainProcess' (12040), thread 'MainThread' (2052):
Traceback (most recent call last):

> File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in my_function

ZeroDivisionError: division by zero
vikramsubramanian commented 2 weeks ago

Hi. Thanks for detailed report.

Stack trace context isn't available when Python is executed in an interactive shell. This is a Python technical limitation and unfortunately, there is nothing I can do to fix it.

vikramsubramanian commented 2 weeks ago

Hi

Thanks for the reply. Alright, the GIF at the very beginning of the readme suggests it should work.

Anyway, I did the following to check if what you said is true.

  1. I created a new file

main.py

from loguru import logger

def my_function(x, y, z):
    hashtag An error? It's caught anyway!
    return 1 / (x + y + z)

def main():
   logger.add("out.log", backtrace=True, diagnose=True)
   my_function(0, 0, 0)

if __name__ == '__main__':
    main()
  1. Activate the conda env
conda activate loguru
  1. And then run from my terminal
python main.py

I then indeed get:

2022-05-23 12:05:39.791 | ERROR    | __main__:main:10 - An error has been caught in function 'main', process 'MainProcess' (23152), thread 'MainThread' (20928):
Traceback (most recent call last):

  File "main.py", line 13, in <module>
    main()
    └ <function main at 0x0000029259088EE0>

> File "main.py", line 10, in main
    my_function(0, 0, 0)
    └ <function my_function at 0x0000029256F024C0>

  File "main.py", line 6, in my_function
    return 1 / (x + y + z)
                │   │   └ 0
                │   └ 0
                └ 0

ZeroDivisionError: division by zero

Which is exactly what I expected.

![image](

vikramsubramanian commented 2 weeks ago

I do not know if many people fall for this, but I suggest to note this behaviour in the readme as a hint or something. It could avoid confusion about this.

Thanks for the quick response!

vikramsubramanian commented 2 weeks ago

Well, you're not alone:

The .gif is indeed misleading, sorry for that. It would be better to change it or add a note underneath, but I can't do that without compromising the desired look of the Readme. I added a note under the "Fully descriptive exceptions" section. Hopefully it will reduce confusion a little.