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:

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

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:"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.