[Tutor] logging module, how to print line numbers?

Hans Fangohr H.FANGOHR at soton.ac.uk
Thu Aug 31 22:14:34 CEST 2006


I have some trouble with the logging module.

When I run this program with python2.3:

import logging,sys
formatter = logging.Formatter('%(name)s :%(asctime)s %(filename)s
%(lineno)s %(levelname)s  %(message)s')
stdout_handler = logging.StreamHandler(sys.stdout)
logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bows')

I get the following output:

root :2006-08-31 20:20:15,085 __init__.py 988 DEBUG  A debug message
root :2006-08-31 20:20:15,085 __init__.py 988 INFO  Some information
root :2006-08-31 20:20:15,085 __init__.py 988 WARNING  A shot across thebows

Note that the line number always appears as 988. I'd like it to be the
line number where the logging command has been executed. The documentation
says that %(lineno)d should work 'if available'.

Is this generally not available? Can I make it available? If so, how?

Any advice welcome.

Many thanks in advance,


More information about the Tutor mailing list