logging: local functions ==> loss of lineno

Peter Otten __peter__ at web.de
Fri Mar 19 08:56:46 CET 2010


Hellmut Weber wrote:

> your hack is exactly what I was looking for.
> It permits to configure my logging messages as I want, e.g. using
> different colors for different classes of messages.
> 
> I do not yet understand all details WHY it is working but suppose some
> study of the logging module will help me to understand.

Have  a look at Logger.findCaller() in logging/__init__.py. To find the 
calling function it does something like

for frame in walk_callstack():
    if filename_of(frame) == _srcfile:
        continue
    return filename_lineno_and_functionname_of(frame)

The _srcfile is normally logging/__init__.py, and it is skipped because a 
user typically is interested only in what happens outside the logging 
package.

My hack under the hood changes

filename_of(frame) == _srcfile

from a string comparison to a

filename_of(frame) in set_of_files

containment test (see SrcFile.__eq__() posted above).

Peter






More information about the Python-list mailing list