Error handling with Python – writing messages out to a text file (log file)


Debugging geoprocessing scripts in Python is never easy, especially when these scripts are run as a scheduled task.
I’ve had a number of requests of how to debug a scheduled python script (especially when issues occur), and write the output to a text file.

Please find below a simple python script that I hope will help others to debug their scripts:


import sys, string, os, arcpy, calendar, datetime, traceback
from arcpy import env
try:
d = datetime.datetime.now()
 log = open("C:\\Temp\PythonOutputLogFile.txt","a")
 log.write("----------------------------" + "\n")
 log.write("----------------------------" + "\n")
 log.write("Log: " + str(d) + "\n")
 log.write("\n")
# Start process...
 starttime = datetime.datetime.now()
 log.write("Begin process:\n")
 log.write("     Process started at " 
           + str(starttime) + "\n")
 log.write("\n")
# Your GP tool code goes here
# .....
endtime = datetime.datetime.now()
# Process Completed...
 log.write("     Completed successfully in " 
           + str(endtime - starttime) + "\n")
 log.write("\n")
 log.close()
except:
 # Get the traceback object
 tb = sys.exc_info()[2]
 tbinfo = traceback.format_tb(tb)[0]
 # Concatenate information together concerning 
 # the error into a message string
 pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + 
         "\nError Info:\n" + str(sys.exc_info()[1])
 msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
# Return python error messages for use in 
# script tool or Python Window
 arcpy.AddError(pymsg)
 arcpy.AddError(msgs)
# Print Python error messages for use in 
# Python / Python Window
 log.write("" + pymsg + "\n")
 log.write("" + msgs + "")
 log.close()

 


Output example:

----------------------------
----------------------------
Log: 2014-03-21 15:13:07.265000
Begin process:
        Process started at 2014-03-21 15:13:07.266000
        Completed successfully in 0:00:00

 

Tania T.

3 thoughts on “Error handling with Python – writing messages out to a text file (log file)

  1. Leland Snyder

    I tried this out for a scheduled task, so thank you for this post. This is catching the second of three tasks, the first one being successfull and second one not. One question, I am trying to catch errors in a single script to (1)compress, (2)rebuild indexes and (3)analyze my enterprise gdb. With that being said, how can I run error checking if one process in the middle of the three cuts to the except line and I don’t get to see whether or not my third process will execute? Would I have to use “try: except”: in a for loop? Or just wrap each task with a try:

    Thanks

    Reply

Got something to say?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s