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

Hans Fangohr H.FANGOHR at soton.ac.uk
Fri Sep 1 08:18:22 CEST 2006


Hi Kent,

> > 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
Interesting, thank you.
>
> How are you running the program? What OS? What Python (2.3.??)
This was Debian Etch, python 2.3.4. I got the same problem with python
2.4.4 on the same system.

I have just tried this on Mac OS X (with python from fink), and it works
fine there.

> 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__?
This was useful information. I played around with the __init__.py file and
the relevant code in there (around line 970).

It turns out that the problem disappears if I remove the

As a result, python needs to read __init.py which appears to work
correctly.

So in summary, it seems that the precompiled
/usr/lib/python2.3/logging/__init__.pyc file was somehow buggy.

I have tried to reproduce the same problem on another machine with Debian
Etch, and it doesn't exist there.

To confuse matters further, (as stated above), I get the same error on
that 'faulty' machine with python2.4.

In summary, the code I emailed initially works fine but it seems that on
the machine I used for testing something was broken with the precompiled
__init__.py file of the logging module. This seems to be an issue with the
debian package.

Many thanks to all who replied, and in particular to Kent who put me on
the right track of solving the problem.

Cheers,

Hans



>
> 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
> >
> >
> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

--
Hans Fangohr
School of Engineering Sciences
University of Southampton
Phone: +44 (0) 238059 8345

Email: fangohr at soton.ac.uk
http://www.soton.ac.uk/~fangohr






More information about the Tutor mailing list