add sendxmpp error handler

This commit is contained in:
Trent Palmer 2021-03-02 20:20:39 -08:00
parent 8cf2ba0edf
commit 9d47561b0b
3 changed files with 51 additions and 0 deletions

12
tp/logging_settings.py Normal file
View File

@ -0,0 +1,12 @@
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": "tp.sendxmpp_handler.SENDXMPPHandler"
}
logging_dict['loggers']['django']['handlers'].append("send_xmpp")
return logging_dict

29
tp/sendxmpp_handler.py Normal file
View File

@ -0,0 +1,29 @@
from logging import Handler
from os import popen
from django.conf import settings
LOGGING_XMPP_CONFIG = settings.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)

View File

@ -1,4 +1,5 @@
from pathlib import Path from pathlib import Path
from .logging_settings import init_logging_settings
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
@ -65,6 +66,14 @@ TEMPLATES = [
WSGI_APPLICATION = 'tp.wsgi.application' WSGI_APPLICATION = 'tp.wsgi.application'
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')),
'LOGGING_XMPP_COMMAND': str(os.getenv('LOGGING_XMPP_COMMAND')),
'LOGGING_XMPP_USE_TLS': str(os.getenv('LOGGING_XMPP_USE_TLS'))
}
# Database # Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
@ -139,3 +148,4 @@ IMAGES_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{PUBLIC_IMAGES_LOCATION}/'
IMAGES_FILE_STORAGE = 'tp.storage_backends.PublicImageStorage' IMAGES_FILE_STORAGE = 'tp.storage_backends.PublicImageStorage'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),) STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
LOGGING = init_logging_settings()