Created
May 4, 2015 08:47
-
-
Save rod-dot-codes/b87cfaafdb639d985a48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from logging.handlers import SysLogHandler | |
from django.conf import settings | |
import json | |
import socket | |
from base64 import standard_b64encode | |
from Crypto.Cipher import AES | |
from Crypto import Random | |
def pad(x, n=16): | |
p = n - (len(x) % n) | |
return x + chr(p) * p | |
class JSONSysLogHandler(SysLogHandler): | |
def emit(self, record): | |
try: | |
json_dict = json.dumps({ | |
"tag":settings.SITE_URL, | |
"time":record.created, | |
"record": { | |
"created":record.created, | |
"site":settings.SITE_URL, | |
"filename":record.filename, | |
"line":record.lineno, | |
"levelname":record.levelname, | |
"levelno":record.levelno, | |
"message":record.msg, | |
"msecs":record.msecs, | |
"tags":[settings.SITE_URL,"django","python"]} | |
}) | |
en = AES.new( key=settings.ENCRYPT_LOGGER_KEY, | |
mode=AES.MODE_CBC, | |
IV="0" * 16, | |
segment_size=128) | |
cipher = en.encrypt(pad(json_dict)) | |
cipher64 = standard_b64encode(cipher) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | |
sock.sendto(cipher64, (self.address[0], self.address[1])) | |
except: | |
pass #Don't raise an exception since it will loop. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment