Masthead

Debugging With Log Files

1. Introduction

Debugging batch scripts that may be processing hundreds or even thousands of files can be very challenging. One of the standard approaches is to continually append messages on the scripts progress to a text file. Then you can check the text file for progress and errors.

1. Using Python's Logging Class

Python includes a "logging" class that makes it easy to update a log file. The code below will setup a logging session. The code is from: http://docs.python.org/release/2.3.5/lib/node304.html.

import logging
logger = logging.getLogger('myapp')
hdlr = logging.FileHandler('C:/Logs/MyApp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
logger.setLevel(logging.WARNING)

You may want to put this code into a function, possibly in your own "Utility" module.

Once you have executed the code above you can use simple, one line, calls to append text to the log file. Examples include:

logger.info("Completed aspect transform")

logger.error("Aspect transform generated an error")

Try logging errors for yourself.

Additional Resources

Python Library Reference: Basic Logging Example

© Copyright 2018 HSU - All rights reserved.