Python, , Logger.findCaller, , logging.py. - self.log CheloExtendedLogger.warnpfx .
, logging.py , : findCaller , logging.py, , ( , , , ). :
class CheloExtendedLogger(logging.Logger):
[...]
def findCaller(self):
"""
Find the stack frame of the caller so that we can note the source
file name, line number and function name.
"""
f = logging.currentframe().f_back
rv = "(unknown file)", 0, "(unknown function)"
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename in (_srcfile, logging._srcfile):
f = f.f_back
continue
rv = (filename, f.f_lineno, co.co_name)
break
return rv
_srcfile . , logging.py , , -:
if hasattr(sys, 'frozen'):
_srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:])
elif string.lower(__file__[-4:]) in ['.pyc', '.pyo']:
_srcfile = __file__[:-4] + '.py'
else:
_srcfile = __file__
_srcfile = os.path.normcase(_srcfile)
, , , .
, :
2011-02-10 16:41:48,108 [test: lg.py,16] INFO message
2011-02-10 16:41:48,171 [test: lg.py,17] ! PFXWRN warning with prefix
, , logging.Logger, , . , , , .