logging->logger wg. multiprocessing
authorSandro Knauß <knauss@netzguerilla.net>
Fri, 27 Nov 2009 01:09:34 +0100
changeset 23 0180b538ed74
parent 22 8b363361ba55
child 25 7295e27e8c99
child 27 e33b90f96e0f
logging->logger wg. multiprocessing
iro/anbieter/sipgate.py
iro/anbieter/smstrade.py
iro/anbieter/smtp.py
iro/iro.py
iro/job.py
iro/worker.py
--- a/iro/anbieter/sipgate.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/anbieter/sipgate.py	Fri Nov 27 01:09:34 2009 +0100
@@ -17,6 +17,8 @@
 import ConfigParser
 import xmlrpclib
 import base64
+import logging
+logger=logging.getLogger("sipgate")
 
 class NoValidStatusCode(Exception):
     pass
@@ -41,8 +43,7 @@
 
     def sendSMS(self,sms,recipients):
         """send SMS with $sms to $recipients"""
-        import logging
-        logging.debug('sipgate.sendSMS(%s,%s)'%(sms,  str(recipients)))
+        logger.debug('sipgate.sendSMS(%s,%s)'%(sms,  str(recipients)))
         args={
                 "TOS" : "text",
                 "Content" : sms.content
@@ -51,8 +52,7 @@
 
     def sendFAX(self,fax,recipients):
         """send the PDF file $fax  to $recipients"""
-        import logging
-        logging.debug('sipgate.sendFAX(%s,%s)'%(fax,  str(recipients)))
+        logger.debug('sipgate.sendFAX(%s,%s)'%(fax,  str(recipients)))
         pdf=open(fax.attachments[0],"rb")
         args={
                 "TOS" : "fax",
--- a/iro/anbieter/smstrade.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/anbieter/smstrade.py	Fri Nov 27 01:09:34 2009 +0100
@@ -21,6 +21,9 @@
 import gsm0338
 import urllib, httplib
 
+import logging
+logger=logging.getLogger("smstrade")
+
 class UnknownStatusCode(Exception):
     def __init__(self,code):
         self.code=code
@@ -88,8 +91,7 @@
 
     def sendSMS(self,sms,recipients):
         """send SMS with $sms to $recipients"""
-        import logging
-        logging.debug('smstrade.sendSMS(%s,%s)'%(sms,  str(recipients)))
+        logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  str(recipients)))
         sended = []
         route = unicode(self.route)
         message = sms.content
@@ -106,6 +108,7 @@
                 else:
                     route=unicode("economy")
                 smsSendStatus = self.__send(route, to, message, timestamp)	
+                logger.debug('smstrade._send(...)=%i(%s)'%(int(smsSendStatus),str(smsSendStatus)))
                 if int(smsSendStatus) in(100, 999):
                     self.updateStatus(arranged=recipient)
                 else:
@@ -117,8 +120,7 @@
         """ This function is the main part of the request to the sms service.    
         The function has to return a unicode formated string that will represent the answer of the sms service
         to the request."""
-        import logging
-        logging.debug('smstrade._send(%s,%s,%s,%s)'%( route, to, message, timestamp))
+        logger.debug('smstrade._send(%s,%s,%s,%s)'%( route, to, message, timestamp))
         parameters= {"key": self.key,
                 "route": route,
                 "to": to,
@@ -135,7 +137,7 @@
 
         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
         params = "&".join( ["%s=%s" % (urllib.quote(k),urllib.quote(v.encode("utf-8"))) for (k, v) in parameters.items()])
-        logging.debug('smstrade._send-parameters:%s\n\t->%s'%(str(parameters), str(params)) )
+        logger.debug('smstrade._send-parameters:%s\n\t->%s'%(str(parameters), str(params)) )
         headers = {"Content-type": "application/x-www-form-urlencoded",
             "Accept": "text/plain"}
         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
--- a/iro/anbieter/smtp.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/anbieter/smtp.py	Fri Nov 27 01:09:34 2009 +0100
@@ -15,6 +15,8 @@
     default_conf = ''    # override this
 import smtplib
 import ConfigParser
+import logging
+logger=logging.getLogger("SMTP")
 
 class SMTP():
     def __init__(self,config_filename=None,section="smtp"):
@@ -65,8 +67,7 @@
         self.bStart=True
 
     def sendMail(self,mail,recipients):   
-        import logging
-        logging.debug('SMTP.sendMail(%s,%s)'%(mail,  str(recipients)))
+        logger.debug('SMTP.sendMail(%s,%s)'%(mail,  str(recipients)))
         if not self.bStart:
             self.prepareSMTP()
 
@@ -75,7 +76,7 @@
         while len(recipients) > 0:
             tmp_recipients=recipients[:self.max_recipients]
             mail.content['To']=", ".join(tmp_recipients)
-            logging.debug('self.smtp.sendmail(%s,%s,%s)'%(self.send_from,  str(tmp_recipients), mail.as_string()))
+            logger.debug('self.smtp.sendmail(%s,%s,%s)'%(self.send_from,  str(tmp_recipients), mail.as_string()))
             self.smtp.sendmail(self.send_from,  tmp_recipients, mail.as_string())
             self.updateStatus( arranged=tmp_recipients)
             recipients = recipients[self.max_recipients:]
--- a/iro/iro.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/iro.py	Fri Nov 27 01:09:34 2009 +0100
@@ -11,9 +11,10 @@
 #You should have received a copy of the GNU General Public License
 #along with this program; if not, see <http://www.gnu.org/licenses/>.
 
-import multiprocessing, logging
+import logging,multiprocessing
 #logging anfangen
-logging.basicConfig(level=logging.DEBUG, format='%(levelname)s-[%(asctime)s %(processName)s (%(threadName)s)]%(message)s')
+logger=logging.getLogger("iro")
+logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s')
 
 
 # Server code
@@ -142,15 +143,15 @@
                                       certificate=cert,privatekey=key)
     server.relam="xmlrpc"
     
-    logging.info('Server gestartet...')
+    logger.info('Server gestartet...')
     try:
         server.serve_forever()
     except KeyboardInterrupt:
         pass
     except:
-        logging.exception('Äh, ein Fehler ist aufgetreten')
+        logger.exception('Äh, ein Fehler ist aufgetreten')
     finally:    
-        logging.info('Server wird beendet...')
+        logger.info('Server wird beendet...')
         queue.close()
         worker.terminate()
 
--- a/iro/job.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/job.py	Fri Nov 27 01:09:34 2009 +0100
@@ -11,6 +11,8 @@
 #You should have received a copy of the GNU General Public License
 #along with this program; if not, see <http://www.gnu.org/licenses/>.
 
+import logging
+logger=logging.getLogger("job")
 
 class Job(object):
     '''
@@ -42,16 +44,14 @@
         return None
         
     def addGood(self, good):
-        import logging
-        logging.debug('Job(%d)-send to %s'%(self.id, str(good)))
+        logger.debug('Job(%d)-send to %s'%(self.id, str(good)))
         if type(good) == list:
             self.dStatus['good']=self.dStatus['good']+good
         else:
             self.dStatus['good'].append(good)
     
     def addFailed(self, failed):
-        import logging
-        logging.debug('Job(%d)-faild to send to %s'%(self.id, str(failed)))
+        logger.debug('Job(%d)-faild to send to %s'%(self.id, str(failed)))
         if type(failed) == list:
             self.dStatus['failed']=self.dStatus['failed']+failed
         else:
--- a/iro/worker.py	Fri Nov 27 01:07:44 2009 +0100
+++ b/iro/worker.py	Fri Nov 27 01:09:34 2009 +0100
@@ -3,6 +3,7 @@
 
 from multiprocessing import Process
 import logging
+logger = logging.getLogger("iro.worker")
 import time
 
 class Worker(Process):
@@ -11,16 +12,16 @@
         self.queue=queue
         
     def run(self):
-        logging.info('Worker thread läuft nun...')
+        logger.info('Worker thread läuft nun...')
         id=0
         while 1:
             job=self.queue.get()
             if job is None:
                 break # reached end of queue
             id+=1
-            logging.info('ein neuer Job(%d)' %(id))
+            logger.info('ein neuer Job(%d)' %(id))
             try:
                 job.start(id)
-                logging.info('Job(%d) fertig ;)'%(id))
+                logger.info('Job(%d) fertig ;)'%(id))
             except:
-                logging.exception('Job(%d) fehlgeschlagen :('%(id))
+                logger.exception('Job(%d) fehlgeschlagen :('%(id))