using the logging module from pythonista
I have been taken to task to the indescriminate use of
try: except:blocks to handle "edge conditions" in my code. Mea culpa. So, I tried to incorporate logging. How do I force the log file to not be the console?
Here's what did. What am I missing?
import sys import traceback,logging logging.basicConfig(filename = 'log') exception_logger = logging.getLogger('log.exception') def log_traceback(ex, ex_traceback): tb_lines = traceback.format_exception(ex.__class__, ex, ex_traceback) tb_text = ''.join(tb_lines) print tb_text exception_logger.log(0,tb_text) . . . try: self.items[self.currentRow]['accessory_type'] = 'none' # un-flags current selected row except Exception as ex: #needed for very first selection _, _, ex_traceback = sys.exc_info() log_traceback(ex, ex_traceback)
@polymerchm, Please accept my apologies. I was not trying to denigrate your code. We have all been impressed by your substantial contributions to this forum. We are all attempting to learn together.
try: except:is very Pythonic as you say so I do not want to discourage its use. As I said in my post, the author's ideas were a bit overboard for non-production code but his point about a bare
except: passis an interesting one.
In your example, would
except TypeError: passbe sufficient for catching the error that you expect while continuing to raise all unexpected errors? I doubt that resorting to
loggingin necessary in your example. That being said, I can no longer get logging to work in Pythonista and a simple logging example that used to work for me no longer does.
@ccc No offense taken. Always learning.
Logging broken. Hmmm. Good thing I tested it on something simpler. @OMZ. Logging would be nice.
Interesting to know, I'll look into it... I think it might have to do with the whole exception redirection that's going on to show the error markers in the editor, but I haven't checked yet.
I was able to get a logging
.outfile using this example I found on Google.
import logging LOG_FILENAME = 'logging_example.out' logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG, ) logging.debug('This message should go to the log file') f = open(LOG_FILENAME, 'rt') try: body = f.read() finally: f.close() print 'FILE:' print body
Pythonista will say it cannot open the file, but StaSH will open it using
cat. It will also show the logs in the console area.
Hope this helps.
The above works also within a
try: except:block and runs a statement after the except clause as well. It creates and appends to the log file. @blmacbeth: make the extension .txt and the editor reads it just fine.
import logging,sys,traceback LOG_FILENAME = 'logging_example.txt' logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG, ) def log_traceback(ex, ex_traceback): tb_lines = traceback.format_exception(ex.__class__, ex, ex_traceback) tb_text = ''.join(tb_lines) logging.debug(tb_text) logging.debug('This message should go to the log file') list = "this is a test".split() try: print list except Exception as ex: _, _, ex_traceback = sys.exc_info() log_traceback(ex, ex_traceback) logging.debug("got past the exception")
Here is the log file output:
DEBUG:root:This message should go to the log file DEBUG:root:Traceback (most recent call last): File "/var/mobile/Containers/Data/Application/3469D264-D1AC-451E-9E4A-B3E38AD33B7F/Documents/chordcalc/test/Untitled.py", line 18, in <module> print list IndexError: list index out of range DEBUG:root:got past the exception
Two things i learned (I'm attracted to the idea of
logging, but never taken the leap) from the comments section of ccc's link...
you could use
logging.exceptiondirectly, which will include the traceback along with whatever message you include.
if you did want to log the exception at a lower severity level, you could use the
exc_info=Trueargument to logging.debug, etc, which will automatically log the traceback.
(this could avoid the need for your own traceback logger function)