Create a separate logger for the class

This commit is contained in:
Alexei Sorokin 2017-05-28 20:38:41 +03:00
parent 6094b074cc
commit ae3312b2e2
1 changed files with 16 additions and 17 deletions

View File

@ -32,6 +32,10 @@ class Hptoad:
self.client.register_plugin("xep_0045") # XMPP MUC. self.client.register_plugin("xep_0045") # XMPP MUC.
self.muc_obj = self.client.plugin["xep_0045"] 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.jid = opts["jid"]
self.connect = opts["connect"] self.connect = opts["connect"]
self.muc = opts["muc"] self.muc = opts["muc"]
@ -51,13 +55,11 @@ class Hptoad:
msg="Replaced by new connection") msg="Replaced by new connection")
self.muc_obj.joinMUC(self.muc, self.bot_nick, wait=True) self.muc_obj.joinMUC(self.muc, self.bot_nick, wait=True)
@classmethod def log_exception(self, ex):
def log_exception(cls, ex): self.logger.error("%s: %s" % (type(ex).__name__, str(ex)))
logging.error("%s: %s" % (type(ex).__name__, str(ex)))
@classmethod def log_message_event(self, event):
def log_message_event(cls, event): self.logger.debug("&{{jabber:client message} %s %s %s %s %s { }}" %
logging.debug("&{{jabber:client message} %s %s %s %s %s { }}" %
(event["from"], event["id"], event["to"], (event["from"], event["id"], event["to"],
event["type"], event["body"])) event["type"], event["body"]))
@ -190,7 +192,7 @@ class Hptoad:
is_admin=is_admin) is_admin=is_admin)
if err: if err:
logging.error(err) self.logger.error(err)
if is_admin: if is_admin:
self.client.send_message(mto=from_id, mbody=err, mtype="chat") self.client.send_message(mto=from_id, mbody=err, mtype="chat")
if reply: if reply:
@ -256,7 +258,7 @@ class Hptoad:
if self.connect: if self.connect:
connect = self.connect.split(":", 1) connect = self.connect.split(":", 1)
if len(connect) != 2 or not connect[1].isdigit(): if len(connect) != 2 or not connect[1].isdigit():
logging.critical("Conn: Connection server format is " + self.logger.critical("Conn: Connection server format is " +
"invalid, should be example.org:5222") "invalid, should be example.org:5222")
sys.exit(1) sys.exit(1)
else: else:
@ -266,7 +268,7 @@ class Hptoad:
self.register_handlers() self.register_handlers()
self.client.process(block=True) self.client.process(block=True)
else: else:
logging.critical("Auth: Could not connect to server, or " + self.logger.critical("Auth: Could not connect to server, or " +
"password mismatch!") "password mismatch!")
sys.exit(1) sys.exit(1)
@ -274,11 +276,8 @@ class Hptoad:
if __name__ == "__main__": if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL)
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(format="%(asctime)s %(message)s",
format="%(asctime)s %(message)s",
datefmt="%Y/%m/%d %H:%M:%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]): if os.path.isfile(sys.argv[0]) and os.path.dirname(sys.argv[0]):
os.chdir(os.path.dirname(sys.argv[0])) os.chdir(os.path.dirname(sys.argv[0]))