[Tutor] logging module, how to print line numbers?
Kent Johnson
kent37 at tds.net
Fri Sep 1 01:19:46 CEST 2006
Hans Fangohr wrote:
> Hi,
>
> 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)
> stdout_handler.setFormatter(formatter)
> logger=logging.getLogger('')
> logger.addHandler(stdout_handler)
> logger.setLevel(logging.DEBUG)
> 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'.
>
When I run your program with Python 2.3.4 on WinXP I get the expected
output:
root :2006-08-31 18:51:27,046 logging.py 8 DEBUG A debug message
root :2006-08-31 18:51:27,046 logging.py 9 INFO Some information
root :2006-08-31 18:51:27,046 logging.py 10 WARNING A shot across the bows
How are you running the program? What OS? What Python (2.3.??)
Looking at the source (Python23\Lib\logging\__init__.py), the line
number is pulled out of the stack by walking up the stack looking for a
frame whose filename is different from _srcfile. What do you get if you
print logging._srcfile and logging.__file__?
Kent
> Is this generally not available? Can I make it available? If so, how?
>
> Any advice welcome.
>
> Many thanks in advance,
>
> Hans
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
More information about the Tutor
mailing list