Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SENDXMPPHandler for Python logging

This is for logging from a flask app with sendxmpp, because prosody is far easier to set up compared to setting up an email server.

Not only is prosody easy to setup, but it is very well-documented, has it's own ppa, and it's also trivial to add a hook for Let's Encrypt Certs in your certbot renew cron.

I find that poezio is a great xmpp client on Linux, and Yaxim is great on Android.

Also, here's the manpage for sendxmpp.

#!/usr/bin/env python3
# app/sendxmpp_handler.py
from logging import Handler
from os import popen
class SENDXMPPHandler(Handler):
def __init__(
self,
logging_xmpp_server,
logging_xmpp_sender,
logging_xmpp_password,
logging_xmpp_recipient,
logging_xmpp_command,
logging_xmpp_use_tls
):
Handler.__init__(self)
self.logging_xmpp_server = logging_xmpp_server
self.logging_xmpp_sender = logging_xmpp_sender
self.logging_xmpp_password = logging_xmpp_password
self.logging_xmpp_recipient = logging_xmpp_recipient
self.logging_xmpp_command = logging_xmpp_command
self.logging_xmpp_use_tls = logging_xmpp_use_tls
'''
This works on Debian 10 with flask running under gunicorn3 as a systemd service, hack as necessary
echo '<message>' | /usr/bin/sendxmpp -t -u <sender> -j <server> -p <password> <recipient@example.com>
'''
def emit(self, record):
try:
message = self.format(record)
shell_command = "echo '{}' | {} -u {} -j {} -p {} {}".format(
message,
self.logging_xmpp_command,
self.logging_xmpp_sender,
self.logging_xmpp_server,
self.logging_xmpp_password,
self.logging_xmpp_recipient
)
if self.logging_xmpp_use_tls == '1':
shell_command += ' -t'
p = popen(shell_command, "w")
status = p.close()
if status:
print("sendxmpp_handler exit status", status)
except Exception:
self.handleError(record)
#!/usr/bin/env python3
# app/__init__.py
import logging
from flask import Flask
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
from app.sendxmpp_handler import SENDXMPPHandler
from app import routes
if app.config['LOGGING_XMPP_SERVER']:
sendxmpp_handler = SENDXMPPHandler(
logging_xmpp_server=(app.config['LOGGING_XMPP_SERVER']),
logging_xmpp_sender=(app.config['LOGGING_XMPP_SENDER']),
logging_xmpp_password=(app.config['LOGGING_XMPP_PASSWORD']),
logging_xmpp_recipient=(app.config['LOGGING_XMPP_RECIPIENT']),
logging_xmpp_command=(app.config['LOGGING_XMPP_COMMAND']),
logging_xmpp_use_tls=(app.config['LOGGING_XMPP_USE_TLS']),
)
sendxmpp_handler.setLevel(logging.ERROR)
app.logger.addHandler(sendxmpp_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment