[Tutor] More logging probs ...

Kent Johnson kent37 at tds.net
Sat Nov 17 13:35:27 CET 2007


dave selby wrote:
> Im having a bad day. The logging module refused to send anything to
> syslog no matter what I did, so discovering the syslog module &
> thought, for what I need I will write a simple class to do the job.
> 
>  class kmotion_logger:
> 
>     def __init__(self, ident, min_priority):
>         # min_priority must be one of ...
>         # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> 
>         self.case = {'EMERG': syslog.LOG_EMERG,
>                             'ALERT': syslog.LOG_ALERT,
>                             'CRIT': syslog.LOG_CRIT,
>                             'ERR': syslog.LOG_ERR,
>                             'WARNING': syslog.LOG_WARNING,
>                             'NOTICE': syslog.LOG_NOTICE,
>                             'INFO': syslog.LOG_INFO,
>                             'DEBUG': syslog.LOG_DEBUG}
> 
>         self.ident = ident
>         print 'log up to & inclusive of ... ', self.case[min_priority]
>         syslog.setlogmask(syslog.LOG_UPTO(self.case[min_priority]))
> 
>     def log(self, msg, priority):
>         print 'sending message at level ...',  self.case[priority]
>         syslog.openlog(self.ident , syslog.LOG_PID,
> (self.case[priority] | syslog.LOG_USER))

| is a bit-wise or, not a logical or.

Logical or is not correct here anyway because self.case[priority] will 
raise KeyError for an unknown priority. Try
   self.case.get(priority, syslog.LOG_USER)
if you want to provide a default value for unknown keys.

Kent

>         syslog.syslog(msg)
>         syslog.closelog()
> 
> And call it with ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "DEBUG")
> logger.log("TESTING", "ALERT")
> 
> And it worked as I wanted, it logs to syslog (cheers, jumps for joy) :)
> 
> Then I noticed several inconsistencys, the following also works AOK ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "INFO")
> logger.log("TESTING", "ALERT")
> 
> But the next one fails to log ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "NOTICE")
> logger.log("TESTING", "ALERT")
> ALERT is above NOTICE & should log .... I am suspicious of
> '(self.case[priority] | syslog.LOG_USER)' although it looks right and
> have tried LOG_LOCAL6 etc but still no joy
> 
> I have even tried explaining it to my cat, no joy
> 
> Any ideas anyone ?
> 
> Cheers
> 
> A very log frustrated programmer
> 
> Dave
> 
> 
> 
> 



More information about the Tutor mailing list