Error Logging with Python

True, like any other scripting language you can easily open a file for writing and log events to the file. Python does have a specific module for this, though...and it makes life much easier.

On a recent project, I was having quite a bit of trouble with a large data load. The data just was not wanting to cooperate. I needed to combine around 30 data objects (tables and feature classes) into a single geodatabase.

Initially, when starting, I really needed a lot of logging information to troubleshoot my script. Later, once the script was starting to cooperate, I needed slightly less. Once running fairly smoothly, I only needed messages when things went wrong in a dastardly way.

This project led me to rediscover the Python logging module. It was amazing how easy it was to use and accomplish what I needed, tiered logging as the script came together.

How do you use this critter? At the beginning of your script, simply import logging just like any other module. Once imported, set the basic configuration for the script using logging.basicConfig. From there just use the syntax logging.debug,, logging.warning and logging.error. As you troubleshoot your script, you can increase the logging level in the basic config parameters so the script gradually logs less and less information. You can leave your log statements in the script just in case.

The easiest way to get up and running is simply to take a look at the basic tutorial and dive right in. If you are not using it already, you need to be. Happy coding!