[Tutor] Logging module

Dick Moores rdm at rcblue.com
Sun May 11 02:08:18 CEST 2008


At 02:03 PM 5/10/2008, Martin Walsh wrote:
>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

Thank you! That gives me a good start.

Dick



More information about the Tutor mailing list