[Tutor] Logging module

Martin Walsh mwalsh at groktech.org
Sat May 10 23:03:20 CEST 2008


Dick Moores wrote:
> But how to use the logging module to log the report of the screw up?
> Right now I don't care about the various levels. I just want to get
> something into a log file.
> 
> Hellmann suggest this:
> 
> import logging
> LOG_FILENAME = '/tmp/logging_example.out'
> logging.basicConfig(filename=LOG_FILENAME,     level=logging.DEBUG,)
> logging.debug('This message should go to the log file')
> 
> But where to put it? In my above script?

I usually wrap up code in a try except and use the logging.exception
method, which is shorthand for logging.error(msg, exc_info=1), to
include the traceback. Something like this:

import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)

def main():
    a = "qwerty"
    b = a * b

try:
    main()
except:
    logging.exception('unknown')
    raise

HTH,
Marty



More information about the Tutor mailing list