1

I want to create a log of errors that are encountered while running a python script on the raspberry Pi (an embedded module that uses rasbian which is a kind of linux) so that I can inspect it at a later date to see where problems are occurring. I want to append errors each time the script is run, record the date and the error text (eg as printed out by me within an exception or as generated somewhere within python itself)

I guess this must be a common thing to do, so before I go off an write my own, I was wondering if there is a standard way of doing this kind of thing

I have found some suggestions like this, but it doesnt print the date. Also I was wondering if I should pass fsock to functions as an argument or is it ok to use a global ....

fsock = open('my_app_error.log', 'a')
sys.stderr = fsock

fsock.write("URLError")
spiderplant0
  • 3,872
  • 12
  • 52
  • 91
  • 6
    there's an awesome module called logging: http://docs.python.org/2/howto/logging.html#logging-basic-tutorial – vroomfondel Aug 20 '13 at 20:59
  • At the risk of starting a sh*tst*rm, I'd saying using a global for something like an error logging procedure is not out of bounds. Either that or write a routine log_err() that holds onto the file pointer itself. – Jiminion Aug 20 '13 at 21:01
  • @rogaos thanks, just what I was looking for. Do you know if there is a way of temporarily diverting logging messages to the screen without editing every logging.warning() etc? – spiderplant0 Aug 20 '13 at 21:22
  • 1
    answer to my last comment is here: http://stackoverflow.com/questions/13733552/logger-configuration-to-log-to-file-and-print-to-stdout – spiderplant0 Aug 20 '13 at 21:47

1 Answers1

2

You could do something like

def logError(message):
    import inspect
    import logging
    import traceback

    cframe = inspect.currentframe()
    called_from = inspect.getframeinfo(cframe.f_back).function
    try:
        stack_trace = traceback.format_exc()
    except AttributeError:
        stack_trace = 'Unable to retrieve stack_trace.'

    error = '{},{},{},{}\n'.format(message, user, called_from,stack_trace.replace('\n','|'))
    logging.basicConfig(format='%(asctime)s%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',filename='log.csv')
    logging.warning(error)

inspect, logging and traceback modules are useful for error logging

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65