[Tutor] logging module, how to print line numbers?
kent37 at tds.net
Fri Sep 1 13:58:02 CEST 2006
Hans Fangohr wrote:
> Hi Kent (and others),
> On 01/09/06, Kent Johnson <kent37 at tds.net> wrote:
>> How are you running the program? What OS? What Python (2.3.??)
> This is python 2.3.5 on Debian etch. Same results with python 2.4.4 on
> Debian etch (although 'the linenumber' is 1072 instead of 988).
Running from the command line or in IDLE or ??
>> 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__?
> I guess these files should rather be my source file(?). But they aren't.
No, they seem correct, they are the source file of the logging module.
I'm perplexed, the values above seem correct.
The way the logging module finds the line number is, it walks up through
the stack looking for a stack frame that references a file different
from the file containing the module itself. It uses the file and line
number from that stack frame for the log.
For some reason your stack frame has something different. One thing you
could do is add a print to the logging module itself to see what it is
seeing. In Python 2.3, there is a findCaller() method at lin 961 of
logging/__init__.py. You could add
just after the line
filename = os.path.normcase(co.co_filename)
Then run your program again and see what it prints out.
More information about the Tutor