#!/usr/bin/env python # xmpp handler for logging # Copyright 2008 Yury Yurevich # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation, version 2 # of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. """ xmpplog - XMPP handler for logging (based on pyxmpp) Example: import logging import xmpplog logger = logging.getLogger("simple_example") logger.setLevel(logging.DEBUG) x = xmpplog.XMPPHandler('jid@from.jabber.org', u'some_pass', 'jid@to.jabber.org') x.setLevel(logging.WARNING) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") x.setFormatter(formatter) logger.addHandler(x) # "application" code logger.debug("debug message") logger.info("info message") logger.warn("warn message") logger.error("error message") logger.critical("critical message") """ import logging from pyxmpp.jid import JID from pyxmpp.jabber.simple import send_message class XMPPHandler(logging.Handler): """ A handler class which sends an XMPP message for each logging event. """ def __init__(self, my_jid, my_password, to_jid): logging.Handler.__init__(self) self.my_jid = my_jid self.my_password = my_password self.to_jid = to_jid def emit(self, record): """ Emit a record. Format the record and send it to the specified jid. """ try: my_jid = JID(self.my_jid) if not my_jid.resource: my_jid = JID(my_jid.node, my_jid.domain, "logging") to_jid = JID(self.to_jid) my_password = unicode(self.my_password) msg = unicode(self.format(record)) send_message(my_jid, my_password, to_jid, msg) except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record)