Skip to content

Instantly share code, notes, and snippets.

@chrisfarms
Created March 12, 2011 23:35
Show Gist options
  • Save chrisfarms/867702 to your computer and use it in GitHub Desktop.
Save chrisfarms/867702 to your computer and use it in GitHub Desktop.
Python logging handler to send error logs via XMPP/Jabber
from google.appengine.api import xmpp
import logging
import logging.handlers
import os
DEFAULT_FROM_JID = 'logs@%s.appspotchat.com' % os.environ['APPLICATION_ID']
class XmppLogHandler(logging.Handler):
def __init__(self, jid, from_jid):
logging.Handler.__init__(self)
self.jid = jid
self.from_jid = from_jid
self.is_logging = False
def emit(self, record):
# prevent recursive logging
if self.is_logging:
return
self.is_logging = True
# if user is online send a message
if xmpp.get_presence(self.jid, from_jid=self.from_jid):
status_code = xmpp.send_message(self.jid, self.format(record), from_jid=self.from_jid)
self.is_logging = False
@classmethod
def add(self, jid, from_jid=DEFAULT_FROM_JID, level=logging.ERROR):
if hasattr(logging.handlers,'XmppLogHandler'):
return
# ensure the user is subscribed
xmpp.send_invite(jid, from_jid)
# add handler to logging namespace
logging.handlers.XmppLogHandler = self
# create handler
handler = logging.handlers.XmppLogHandler(jid, from_jid)
handler.setLevel(level)
# add the hadnler
logger = logging.getLogger()
logger.addHandler(handler)
@chrisfarms
Copy link
Author

Somewhere in your application startup (before run_wsgi_app is called) add the following to start sending log messages via jabber:

from lib.xmpplogging import XmppLogHandler
XmppLogHandler.add('yourname@example.com')

Set the level and address the logs come from by passing level and from_jid args:

XmppLogHandler.add('yourname@example.com', level=logging.INFO, from_jid='appid@appspot.com')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment