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.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,13 +55,11 @@ 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 { }}" %
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"]))
@ -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,7 +258,7 @@ 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 " +
self.logger.critical("Conn: Connection server format is " +
"invalid, should be example.org:5222")
sys.exit(1)
else:
@ -266,7 +268,7 @@ class Hptoad:
self.register_handlers()
self.client.process(block=True)
else:
logging.critical("Auth: Could not connect to server, or " +
self.logger.critical("Auth: Could not connect to server, or " +
"password mismatch!")
sys.exit(1)
@ -274,11 +276,8 @@ class Hptoad:
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]))