[PEAK] Request: running.logs.TRACE convenience method

Phillip J. Eby pje at telecommunity.com
Tue Dec 16 19:41:54 EST 2003


At 03:51 PM 12/16/03 -0800, John Landahl wrote:
>For nearly all logging levels one can use a log.<level> convenience method 
>to generate a log event of the given level.  Except for the lowest level 
>(TRACE), which has no corresponding convenience method.
>
>Is there a reason for not including a "log.trace()" convenience method?
>It would be very useful for apps that frequently need to send log events 
>at an even lower level than 'debug'.

peak.running.logs is intended to be directly usable with the Python 2.3 
logging package -- which doesn't support 'trace()' or 'notice()' methods 
for the TRACE and NOTICE levels that PEAK supports.

There is, however, a simple solution...

class IMyLogger(running.ILogger):
     def trace(msg, *args, **kwargs):
         """Log 'msg' w/level TRACE"""

class MyLogger(protocols.Adapter):

     # make sure binding.Delegate will work; must go before advise()
     __metaclass__ = binding.Activator

     protocols.advise(
         instancesProvide=[IMyLogger],
         asAdapterForProtocols=[running.ILogger]
     )

     # Delegate all the normal methods to the wrapped logger
     log = error = exception = debug = warning = info = \
         critical = isEnabledFor = getEffectiveLevel = \
             binding.Delegate('subject')

     def trace(self, msg, *args, **kwargs):
         self.log(logs.TRACE, msg, *args, **kwargs)


...and finally, use 'adaptTo=IMyLogger' in your logger binding.  Note that 
this will be slightly slower for trace messages than other messages, since 
there's a second level of indirection.

Given that this is the second time this issue has come up (the other was 
from a co-worker wanting to have a 'notice()' method), I'm thinking when I 
next update the logs module, I will create an IPEP282Logger interface and 
derive ILogger from that, having running.ILogger add 'trace()' and 
'notice()' methods over the PEP 282 methods.  To ensure PEP 282 
compatibility, I'll create an adapter like the one above, and declare that 
the Python 2.3 logging module's loggers support IPEP282Logger, and I'll 
make the compatibility layer do the adaptation on getLogger().

Anyway, in the meantime you can easily roll your own extension as shown.




More information about the PEAK mailing list