robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Error Logging - Help

Sun Jan 19, 2014 7:57 am

I have just finished writing some code in Python which is designed to run on my RPi without a monitor connected. I expect to need to keep the RPi running this script for upto 60 days.

When I run it via SSH, everything works fine, but now that I have set the code to run at boot-up, I have discovered that it is crashing after 10 hours or so.

Is there any command that I can use, which will log ANY error for the whole script (any def or thread) so that I can track the problem down?

Thanks

Robbo100.

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: Error Logging - Help

Sun Jan 19, 2014 8:50 am

robbo100 wrote:I have just finished writing some code in Python which is designed to run on my RPi without a monitor connected. I expect to need to keep the RPi running this script for upto 60 days.

When I run it via SSH, everything works fine, but now that I have set the code to run at boot-up, I have discovered that it is crashing after 10 hours or so.

Is there any command that I can use, which will log ANY error for the whole script (any def or thread) so that I can track the problem down?

Thanks

Robbo100.

Code: Select all

try:
    #your code
except Exception,e:
    print str(e)
Don't be mean, I'm only fifteen :D

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Error Logging - Help

Sun Jan 19, 2014 9:52 am

Presumably I can use the "print >>" command to send the string to an open file?

ZacharyIgielman
Posts: 101
Joined: Sun Dec 08, 2013 11:27 am
Location: London

Re: Error Logging - Help

Sun Jan 19, 2014 10:25 am

robbo100 wrote:Presumably I can use the "print >>" command to send the string to an open file?
Yes you can!

If you want the whole traceback:

Code: Select all

import traceback

try:
    #code
except Exception, e:
    print str(e) #you could also print to file here
    traceback.print_exc()
Don't be mean, I'm only fifteen :D

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Error Logging - Help

Sun Jan 19, 2014 10:41 am

Thanks mate

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Error Logging - Help

Sun Jan 19, 2014 12:57 pm

If I've got a script I leave running, I either use the logging module as I can set various debug levels, or do something simple like this:

Code: Select all

from datetime import datetime

def scriptdebug(errormessage):
    with open("error.log", "a") as errorlog:
        errorlog.write("%s\t%s\n" % (datetime.strftime(datetime.now(),
                                     "%d-%m-%y %H:%M"),
                                     errormessage))
You can then use it as Zachary suggests:

Code: Select all

try:
    #code
except Exception, e:
    scriptdebug(e)
Error messages will be posted to the "error.log" file with a timestamp of the current date and time.

EDIT: Should also add that if you want to use this method in conjunction with the traceback module as Zachary suggested (and the advantage of this is that you do get more detail on the error) then, assuming you've imported the traceback module, you'd call the scriptdebug method like this:

Code: Select all

scriptdebug(traceback.format_exc())
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”