ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Logging to a Jabber account
Submitter: Willi Richert (other recipes)
Last Updated: 2006/10/03
Version no: 1.0
Category: Programs

 

5 stars 1 vote(s)


Description:

If you have long-running scripts on many remote machines and you want to be alarmed if one of them crashes, use the JabberHandler to log it to your account.

Source: Text Source

import xmpp # Jabber client/server lib from http://pyxmpp.jajcus.net/

class JabberHandler(logging.Handler):
    """
    A handler class which sends a Jabber message for each logging event.
    """
    def __init__(self, from_id, passwd, to_id):
        logging.Handler.__init__(self)
        
        self.from_id, self.passwd = from_id, passwd
        self.to_id = to_id
        jid=xmpp.JID(self.from_id)
        self.user, self.server = jid.getNode(), jid.getDomain()
    
        conn=xmpp.Client(self.server)#,debug=[])
        conres=conn.connect()
        
        if not conres:
            print "Unable to connect to server %s!"%self.server
            sys.exit(1)
        if conres<>'tls':
            print "Warning: unable to estabilish secure connection - TLS failed!"
        authres=conn.auth(self.user, self.passwd)
        if not authres:
            print "Unable to authorize on %s - check login/password."%self.server
            sys.exit(1)
        if authres<>'sasl':
            print "Warning: unable to perform SASL auth os %s. Old authentication method used!"%self.server
        conn.sendInitPresence()
        self.conn = conn

    def emit(self, record):
        try:
            msg = self.format(record)
            self.conn.send(xmpp.Message(to=self.to_id,body=msg, frm="Logger"))
        except:
            self.handleError(record)

if __name__=="__main__":
    log = logging.getLogger("URGENT")
    log.setLevel(logging.ERROR)
    log.addHandler(JabberHandler("myID@jabber.org", "mypasswd", "myID@jabber.org"))
    log.error("Serious error because of ...")

Discussion:



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.