usr bin env python xmpp handler for logging Copyright 2008 Yury Yurevi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# xmpp handler for logging
# Copyright 2008 Yury Yurevich <the.pythy@gmail.com>
#
# 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)