logging magic

nicholas.farrell at gmail.com nicholas.farrell at gmail.com
Mon Jun 12 10:18:05 EDT 2006


Hi everyone.

I've just been trying to add a bit more granularity to my logging code,
as the jump from INFO (level 20) to DEBUG (level 10) is a bit too big.
I was thinking of borrowing a few levels from java: fine (18), finer
(16) and finest(14).

This is what I've tried:

log = logging.getLogger(appName)

logging.FINE = 18
logging.FINER = 16
logging.FINEST = 14

log.fine = lambda msg, self=log, level=logging.FINE: self.log(level,
msg)
log.finer = lambda msg, self=log, level=logging.FINER: self.log(level,
msg)
log.finest = lambda msg, self=log, level=logging.FINEST:
self.log(level, msg)

I could expand this to support kwargs, etc, but that's the least of my
problems.

The real issue is that by adding this extra method into the stack, the
logging module incorrectly reports the module name/line number. I
assume it's because logging.log(...) accesses the stack to find out
where it was being invoked from.

Does anyone know I can add such methods to my log object and still have
it correctly report the context of the log message?

Thanks to all in advance.




More information about the Python-list mailing list