Logging Custom Levels?

Didymus lynto28 at gmail.com
Wed Apr 1 07:51:29 EDT 2015


On Tuesday, March 31, 2015 at 1:37:29 PM UTC-4, Jean-Michel Pichavant wrote:
> 
> A solution is pretty simple, do not use an intermediate log function pdebug.
> 
> import logging
> PDEBUG_NUM=20
> logging.addLevelName(PDEBUG_NUM, "PDEBUG")
>  
> logger = logging.getLogger('foo')
> logging.basicConfig(level=logging.DEBUG, format='%(message)s %(lineno)d')
> 
> logger.log(PDEBUG_NUM, 'This will work :')
> 
> 
> If you *really* want to go for the hackish way, forget about the inspect module, the following pdebug function should do the trick:
> 
> def pdebug(self, message, *args, **kws):
>     if self.isEnabledFor(PDEBUG_NUM):
>         self._log(PDEBUG_NUM, message, args, **kws)
> 
> Cheers,
> 
> JM
> 

Very good, thank you!
    Tom
 




More information about the Python-list mailing list