initial commit

This commit is contained in:
Trent Palmer 2021-02-03 09:58:49 -08:00
parent 0d8309eed1
commit aaf275b8d1
3 changed files with 66 additions and 1 deletions

View File

@ -1,2 +1,30 @@
# mysite/mysite/django_sendxmpp_handler.py
from logging import Handler from logging import Handler
from os import popen from os import popen
from mysite.settings import LOGGING_XMPP_CONFIG
class SENDXMPPHandler(Handler):
def emit(self, record):
try:
message = self.format(record)
shell_command = "echo '{}' | {} -u {} -j {} -p {} {} --tls-ca-path='/etc/ssl/certs'".format(
message,
LOGGING_XMPP_CONFIG['LOGGING_XMPP_COMMAND'],
LOGGING_XMPP_CONFIG['LOGGING_XMPP_SENDER'],
LOGGING_XMPP_CONFIG['LOGGING_XMPP_SERVER'],
LOGGING_XMPP_CONFIG['LOGGING_XMPP_PASSWORD'],
LOGGING_XMPP_CONFIG['LOGGING_XMPP_RECIPIENT']
)
if LOGGING_XMPP_CONFIG['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)

14
logging_settings.py Normal file
View File

@ -0,0 +1,14 @@
# mysite/mysite/logging_settings.py
from copy import deepcopy
from django.utils.log import DEFAULT_LOGGING
def init_logging_settings():
logging_dict = deepcopy(DEFAULT_LOGGING)
logging_dict['handlers']['send_xmpp'] = {
"level": "ERROR",
"class": "mysite.sendxmpp_handler.SENDXMPPHandler"
}
logging_dict['loggers']['django']['handlers'].append("send_xmpp")
return logging_dict

23
settings.py Normal file
View File

@ -0,0 +1,23 @@
# mysite/mysite/settings.py
from .logging_settings import init_logging_settings
import os
from dotenv import load_dotenv
load_dotenv()
LOGGING_XMPP_CONFIG = {
'LOGGING_XMPP_SERVER': str(os.getenv('LOGGING_XMPP_SERVER')),
'LOGGING_XMPP_SENDER': str(os.getenv('LOGGING_XMPP_SENDER')),
'LOGGING_XMPP_PASSWORD': str(os.getenv('LOGGING_XMPP_PASSWORD')),
'LOGGING_XMPP_RECIPIENT': str(os.getenv('LOGGING_XMPP_RECIPIENT')),
# '/usr/bin/site_perl/sendxmpp' or maybe '/usr/bin/sendxmpp'
'LOGGING_XMPP_COMMAND': str(os.getenv('LOGGING_XMPP_COMMAND')),
'LOGGING_XMPP_USE_TLS': str(os.getenv('LOGGING_XMPP_USE_TLS'))
}
LOGGING = init_logging_settings()