traceback

Python logging: disable stack trace

泪湿孤枕 提交于 2019-12-04 06:16:27
Is there a simple way to disable the logging of an exception stack trace in Python 3, either in a Handler or Formatter ? I need the stack trace in another Handler , so setting exc_info=False , in the call to the Logger is not an option. Is there a simpler way than just defining my own Formatter ? The easiest option to disable per handler traceback output is to add a custom logging.Filter subclass that alters the record object (rather than filter out records). The filter simply has to set exc_info on records to None : class TracebackInfoFilter(logging.Filter): """Clear or restore the exception

How to imitate Python 3's raise … from in Python 2?

十年热恋 提交于 2019-12-03 23:59:12
Python 3 has the neat try: raise OneException('sorry') except OneException as e: # after a failed attempt of mitigation: raise AnotherException('I give up') from e syntax which allows raising a followup exception without loosing context. The best analogy I could come up with in Python 2 is raise AnotherException((e,'I give up')), None, sys.exc_info()[2] where the (e,'') is an ugly hack to have the original exception's name included in the message. But isn't there a better way? There's a raise_from in python-future ; simply install it pip install future and import to use from future.utils

Why can't I pickle an error's Traceback in Python?

房东的猫 提交于 2019-12-03 23:33:47
I've since found a work around, but still want to know the answer. The traceback holds references to the stack frames of each function/method that was called on the current thread, from the topmost-frame on down to the point where the error was raised. Each stack frame also holds references to the local and global variables in effect at the time each function in the stack was called. Since there is no way for pickle to know what to serialize and what to ignore, if you were able to pickle a traceback you'd end up pickling a moving snapshot of the entire application state: as pickle runs, other

traceback from a warning

好久不见. 提交于 2019-12-03 10:37:50
I have a code which, at some point shows a warning, I think that it is having a problem calculating a mean() I would like to know if there is any way to force python to tell me where, or which line, or whatever more information than just this message: C:\Python27\lib\site-packages\numpy\core\_methods.py:55: RuntimeWarning: Mean of empty slice. warnings.warn("Mean of empty slice.", RuntimeWarning) C:\Python27\lib\site-packages\numpy\core\_methods.py:79: RuntimeWarning: Degrees of freedom <= 0 for slice warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning) I do not know if it is

How can I log current line, and stack info with Python?

£可爱£侵袭症+ 提交于 2019-12-03 08:40:27
问题 I have logging function as follows. logging.basicConfig( filename = fileName, format = "%(levelname) -10s %(asctime)s %(message)s", level = logging.DEBUG ) def printinfo(string): if DEBUG: logging.info(string) def printerror(string): if DEBUG: logging.error(string) print string I need to login the line number, stack information. For example: 1: def hello(): 2: goodbye() 3: 4: def goodbye(): 5: printinfo() ---> Line 5: goodbye()/hello() How can I do this with Python? SOLVED def printinfo

How can I log current line, and stack info with Python?

扶醉桌前 提交于 2019-12-03 00:12:19
I have logging function as follows. logging.basicConfig( filename = fileName, format = "%(levelname) -10s %(asctime)s %(message)s", level = logging.DEBUG ) def printinfo(string): if DEBUG: logging.info(string) def printerror(string): if DEBUG: logging.error(string) print string I need to login the line number, stack information. For example: 1: def hello(): 2: goodbye() 3: 4: def goodbye(): 5: printinfo() ---> Line 5: goodbye()/hello() How can I do this with Python? SOLVED def printinfo(string): if DEBUG: frame = inspect.currentframe() stack_trace = traceback.format_stack(frame) logging.debug

File “<string>” in python traceback

本小妞迷上赌 提交于 2019-12-02 05:33:00
问题 I am in the middle of refactoring a huge py module to packages - to not break existing code I moved its contents to package/__init__.py module (Adding code to __init__.py) and went on splitting it from there. I noticed at some point that in my tracebacks I get: Traceback (most recent call last): File "<string>", line 656, in DoItemMenu File "bash\balt.py", line 2109, in PopupMenu link.AppendToMenu(menu,parent,*args) File "bash\balt.py", line 2225, in AppendToMenu for link in self.links: link

how to catch all uncaught exceptions and go on?

爷,独闯天下 提交于 2019-12-02 04:53:51
问题 EDIT : after reading the comments and the answers I realize that what I want to do does not make much sense. What I had in mind was that I have some places in my code which may fail (usually some requests calls which may not go though) and I wanted to catch them instead of putting a try: everywhere. My specific problem was such that I would not care if they fail and would not influence the rest of the code (say, a watchdog call). I will leave this question for posterity as an ode to "think

Can I make Python output exceptions in one line / via logging?

萝らか妹 提交于 2019-12-01 15:57:50
问题 I am using AWS and use AWS cloudwatch to view logs. While things should not break on AWS, they could. I just had such a case. Then I searched for Traceback and just got the lines Traceback (most recent call last): without the actual traceback. I have a working structured logging setup (see other question) and I would like to get tracebacks in a similar way. So instead of: Traceback (most recent call last): File "/home/math/Desktop/test.py", line 32, in <module> adf NameError: name 'adf' is

Python 3 traceback fails when no exception is active

◇◆丶佛笑我妖孽 提交于 2019-12-01 07:28:09
问题 I noticed that in Python2 when I try to dump the exception stack trace, but there's no active exception, it prints None : Python 2.7.2+ (default, Oct 4 2011, 20:06:09) >>> import traceback >>> traceback.print_exc() None But Python3 fails with some internal problem: Python 3.2.2 (default, Sep 5 2011, 21:17:14) >>> import traceback >>> traceback.print_exc() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.2/traceback.py", line 259, in print_exc print