logging script

1981
6
09-28-2011 05:48 AM
PeterHoffman
Occasional Contributor
I need to see where my scripts are failing and would like to echo all commands and the results to a log file. I am a newbie to python and would apreciate any help.
Thanks.
Tags (2)
0 Kudos
6 Replies
PeterStratton
New Contributor
Have you tried running the script in IDLE (the IDE included with your python distro) and see what kind of errors it reports?  If you have exception handling conditionals set up in your script, you could just write the exceptions to a file.  Or, you could use mile marker write statements to chart the progression through your script (i.e. have python write a note to the log file you define each time it successfully passes a certain point).  Read these for more info:

http://docs.python.org/release/2.6.6/tutorial/inputoutput.html#reading-and-writing-files
http://docs.python.org/release/2.6.6/tutorial/errors.html
http://docs.python.org/release/2.6.6/tutorial/stdlib.html#error-output-redirection-and-program-termi...
http://docs.python.org/release/2.6.6/tutorial/stdlib2.html#logging

Hope that helps.
0 Kudos
PeterHoffman
Occasional Contributor
I am running this from a .bat file using task manager at 4AM so that IDE method would not work.
I was looking for something like the echo command used in dos .bat files.
0 Kudos
PeterStratton
New Contributor
If you can't test the python script in IDLE before running it from a batch file, or if the arguments are going to change each time it runs, i'd just write out a log file using try/except statements to record the exceptions before exiting and possibly mile markers to help chart the progress.  You can use string replacement formatting to capture the value of variables or arguments that change each time the script executes:

http://docs.python.org/release/2.6.6/library/string.html#formatstrings
0 Kudos
ChrisSnyder
Regular Contributor III
This is the basic "logger" template I have been using for the last 6 years or so. Here's one for v93 and one for v10.0. By default the log file gets put in the "root" directory, so search for the "root" variable and edit it.
0 Kudos
LoganPugh
Occasional Contributor III
It's also possible to use the logging module (as previously mentioned) which could help to reduce boilerplate code. See this post for an example: http://forums.arcgis.com/threads/4719-Dumb-question-can-I-write-standalone-python-script-to-run-with...
0 Kudos
MarcNakleh
New Contributor III
Full, 100% agreement with Logan. Python has a built-in module for logging information, and it has been tried, tested and fine-tuned to respond to most people's needs.

If you are looking for a means of repeating everything to the screen and to a file, an intermediary function (like the relatively functional showGpMessage in Chris' attachment) would probably be your easiest means of doing so, along with the logger module.

If, for whatever reason, you find that this is not suitable to your needs, you can have a look at overwriting sys.stdout with an object of your choice, which has a method defined for write().
For example:
class GetStream(object):
    def __init__(self, pLog):
        self.terminal = sys.stdout
        self.log = pLog

    def write(self, message):
        self.terminal.write(message)
        try:
            self.log.write(message)
        except UnicodeEncodeError:
            self.log.write(message.encode('LATIN-1'))
        arcpy.AddMessage(message)


Cheers,
Marc
0 Kudos