[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [gettor/develop] Basic logging



commit 888a1de21e3ec7b606d5a456e972d189f73c466c
Author: ilv <ilv@xxxxxxxxxxxxxxxxxxxxxxxx>
Date:   Mon Nov 2 18:01:28 2015 -0300

    Basic logging
---
 gettor/twitter.py |   19 +++++++++----------
 process_tweets.py |   19 ++++++++++++++++---
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/gettor/twitter.py b/gettor/twitter.py
index a197ac1..13666a3 100644
--- a/gettor/twitter.py
+++ b/gettor/twitter.py
@@ -95,7 +95,6 @@ class TwitterBot(object):
             raise InternalError("Core error: %s" % str(e))
 
         # logging
-        """
         log = logging.getLogger(__name__)
 
         logging_format = utils.get_logging_format()
@@ -109,8 +108,8 @@ class TwitterBot(object):
         log.addHandler(logfileh)
 
         # stop logging on stdout from now on
-        log.propagate = False
-        #self.log = log"""
+        #log.propagate = False
+        self.log = log
     
     def _is_blacklisted(self, username):
         """Check if a user is blacklisted.
@@ -221,18 +220,18 @@ class TwitterBot(object):
 
         try:
             if self._is_blacklisted(str(sender_id)):
-                #self.log.info("Request from blacklisted account!")
+                self.log.info("Request from blacklisted account!")
                 status = 'blacklisted'
                 bogus_request = True
 
             if not bogus_request:
-                #self.log.debug("Request seems legit, let's parse it")
+                self.log.debug("Request seems legit, let's parse it")
                 # let's try to guess what the user is asking
                 request = self.parse_text(str(msg))
         
                 # possible options: links, mirrors, help
                 if request['type'] == 'links':
-                    #self.log.debug("Type of request: help")
+                    self.log.debug("Type of request: links")
                     links = self.core.get_links(
                         'Twitter',
                         request['os'],
@@ -244,7 +243,7 @@ class TwitterBot(object):
                     
 
                 elif request['type'] == 'mirrors':
-                    #self.log.debug("Type of request: mirrors")
+                    self.log.debug("Type of request: mirrors")
                     status = 'success'
                     reply = self._get_msg('mirrors', 'en')
                     try:
@@ -256,7 +255,7 @@ class TwitterBot(object):
                         reply = self._get_msg('mirrors_unavailable', 'en')
 
                 else:
-                    #self.log.debug("Type of request: help")
+                    self.log.debug("Type of request: help")
                     status = 'success'
                     reply = self._get_msg('help', 'en')
                 
@@ -267,14 +266,14 @@ class TwitterBot(object):
 
         except (core.ConfigError, core.InternalError) as e:
             # if core failes, send the user an error message, but keep going
-            #self.log.error("Something went wrong internally: %s" % str(e))
+            self.log.error("Something went wrong internally: %s" % str(e))
             status = 'core_error'
             reply = self._get_msg('internal_error', 'en')
 
         finally:
             # keep stats
             if request:
-                #self.log.debug("Adding request to database... ")
+                self.log.debug("Adding request to database... ")
                 self.core.add_request_to_db()    
 
     def start(self):
diff --git a/process_tweets.py b/process_tweets.py
index 484cb97..59c2f48 100644
--- a/process_tweets.py
+++ b/process_tweets.py
@@ -7,16 +7,29 @@ import logging
 import gettor.twitter
 
 def main():
+    logging_level = 'DEBUG'
+    logging_file = '/home/ilv/Proyectos/tor/gettor/log/process_tweets.log'
+    logging_format = '[%(levelname)s] %(asctime)s - %(message)s'
+    date_format = "%Y-%m-%d" # %H:%M:%S
+
+    logging.basicConfig(
+        format=logging_format,
+        datefmt=date_format,
+        filename = logging_file,
+        level = logging_level
+    )
+
+    logging.debug("Starting bot")
     try:
         bot = gettor.twitter.TwitterBot()
         bot.start()
     except gettor.twitter.ConfigError as e:
-        print "Configuration error: %s" % str(e)
+        logging.error("Configuration error: %s" % str(e))
     except gettor.twitter.InternalError as e:
-        print "Core module not working: %s" % str(e)
+        logging.error("Core module not working: %s" % str(e))
     except Exception as e:
         # in case something unexpected happens
-        print "Unexpected error: %s" % str(e)
+        logging.error("Unexpected error: %s" % str(e))
 
 if __name__ == '__main__':
     main()

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits