logging magic

N

nicholas.farrell

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.
 
V

Vinay Sajip

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.

Try using addLevelName (this will ensure that your level names are
correctly formatted in the log), and also try creating your own Logger
subclass which has the custom methods you want.

The logging system does walk up the stack, looking for a stack frame
whose corresponding source file is not the logging package itself. What
is the module name/file number which gets printed?

Regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top