From ae3312b2e25b01a12589af5aac6fc54a023aa88f Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Sun, 28 May 2017 20:38:41 +0300 Subject: [PATCH] Create a separate logger for the class --- hptoad.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/hptoad.py b/hptoad.py index 83be20f..704cd60 100755 --- a/hptoad.py +++ b/hptoad.py @@ -32,6 +32,10 @@ class Hptoad: self.client.register_plugin("xep_0045") # XMPP MUC. self.muc_obj = self.client.plugin["xep_0045"] + self.logger = logging.getLogger(self.__class__.__name__) + self.logger.addHandler(logging.NullHandler()) + self.logger.setLevel(logging.DEBUG) + self.jid = opts["jid"] self.connect = opts["connect"] self.muc = opts["muc"] @@ -51,15 +55,13 @@ class Hptoad: msg="Replaced by new connection") self.muc_obj.joinMUC(self.muc, self.bot_nick, wait=True) - @classmethod - def log_exception(cls, ex): - logging.error("%s: %s" % (type(ex).__name__, str(ex))) + def log_exception(self, ex): + self.logger.error("%s: %s" % (type(ex).__name__, str(ex))) - @classmethod - def log_message_event(cls, event): - logging.debug("&{{jabber:client message} %s %s %s %s %s { }}" % - (event["from"], event["id"], event["to"], - event["type"], event["body"])) + def log_message_event(self, event): + self.logger.debug("&{{jabber:client message} %s %s %s %s %s { }}" % + (event["from"], event["id"], event["to"], + event["type"], event["body"])) def is_muc_admin(self, muc, nick): if nick not in self.muc_obj.rooms[self.muc]: @@ -190,7 +192,7 @@ class Hptoad: is_admin=is_admin) if err: - logging.error(err) + self.logger.error(err) if is_admin: self.client.send_message(mto=from_id, mbody=err, mtype="chat") if reply: @@ -256,8 +258,8 @@ class Hptoad: if self.connect: connect = self.connect.split(":", 1) if len(connect) != 2 or not connect[1].isdigit(): - logging.critical("Conn: Connection server format is " + - "invalid, should be example.org:5222") + self.logger.critical("Conn: Connection server format is " + + "invalid, should be example.org:5222") sys.exit(1) else: connect = () @@ -266,19 +268,16 @@ class Hptoad: self.register_handlers() self.client.process(block=True) else: - logging.critical("Auth: Could not connect to server, or " + - "password mismatch!") + self.logger.critical("Auth: Could not connect to server, or " + + "password mismatch!") sys.exit(1) if __name__ == "__main__": signal.signal(signal.SIGINT, signal.SIG_DFL) - logging.basicConfig(level=logging.DEBUG, - format="%(asctime)s %(message)s", + logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%Y/%m/%d %H:%M:%S") - # Silence sleekxmpp debug information. - logging.getLogger("sleekxmpp").setLevel(logging.CRITICAL) if os.path.isfile(sys.argv[0]) and os.path.dirname(sys.argv[0]): os.chdir(os.path.dirname(sys.argv[0]))